Home » Angularjs » Simple ng-include not working

Simple ng-include not working

Posted by: admin November 7, 2017 Leave a comment

Questions:

Im playing with AngularJS for the first time, and im struggling to use ng-include for my headers and footer.

Here’s my tree:

myApp
assets
   - CSS
   - js
        - controllers
        - vendor
             - angular.js
             - route.js
             ......
             ......
             ......
        main.js
pages
   - partials
        - structure
              header.html
              navigation.html
              footer.html
   index.html
   home.html

index.html:

<!DOCTYPE html>
<html ng-app="app">
<head>
    <title>AngularJS Test</title>

    <script src="/assets/js/vendor/angular.js"></script>
    <script src="/assets/js/vendor/route.js"></script>
    <script src="/assets/js/vendor/resource.js"></script>
    <script src="/assets/js/main.js"></script>

</head>
    <body>          

        <div ng-include src="partials/structure/header.url"></div>
        <div ng-include src="partials/structure/navigation.url"></div>    

        <div id="view" ng-view></div>   

        <div ng-include src="partials/structure/footer.url"></div>
    </body>
</html>

main.js

var app = angular.module("app", ["ngResource", "ngRoute"]);


app.config(function($routeProvider) {

$routeProvider.when("/login", {
    templateUrl: "login.html",
    controller: "LoginController"
});

$routeProvider.when("/home", {
    templateUrl: "home.html",
    controller: "HomeController"
});

$routeProvider.otherwise({ redirectTo: '/home'});

});

app.controller("HomeController", function($scope) {
    $scope.title = "Home";
});

home.html

<div>
<p>Welcome to the {{ title }} Page</p>
</div>

When i go on the home.html page, my header, nav and footer are not appearing.

Answers:

You’re doing an include of header.url instead of header.html. It looks like you want to use literals in the src attribute, so you should wrap them in quotes, as was mentioned in the comments by @DiscGolfer.

Change

 <div ng-include src="partials/structure/header.url"></div>
 <div ng-include src="partials/structure/navigation.url"></div>    

 <div id="view" ng-view></div>   

 <div ng-include src="partials/structure/footer.url"></div>

to

 <div ng-include src="'partials/structure/header.html'"></div>
 <div ng-include src="'partials/structure/navigation.html'"></div>    

 <div id="view" ng-view></div>   

 <div ng-include src="'partials/structure/footer.html'"></div>

If this is not working, check the browser console if there are any 404’s

Questions:
Answers:

I had a similar problem with a simple ng-include not rendering:

<div ng-include="views/footer.html"></div>

I wrapped the file path in single quotes and it now renders:

<div ng-include="'views/footer.html'"></div>

Questions:
Answers:

i had a similar problem that landed me here.

ng-include was not populating the contents of my partial, but there were no console errors, nor 404’s.

then i realized what i did.

fix for me: make sure you have ng-app outside of the ng-include! so obvious. the price of being an Angular noob.

Questions:
Answers:

I just figured this one out!

For me, I was using a CDN for importing my angular.min.js file that apparently wasn’t working. I switched to:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>

and included ng-app="" in my body tag:

<body ng-app="">
<div ng-include="'testfile.html'"></div>

and it’s working fine now. Cheers!

Questions:
Answers:

I also came across this issue. And this is what worked for me.

So instead of writing this:<div ng-include="'html/form.htm'"></div>

You want to add ng-app="". So it should look like this: <div ng-app="" ng-include="'html/form.htm'"></div>

Questions:
Answers:

Yes, without the '', ng would try to evaluate the contents inside the ng-include("")

This evaluation is another reason why you don’t put {{}} inside these directives.

Questions:
Answers:

I was struggling with the same issue and have done almost all what was advised in different stacks but it didn’t work for me. On Console, I was getting the Error:

“Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.”

Later I realised, I have to use a local web server (MAMP, WAMP etc.) to make it work.

Please be careful of the above error message. Chances are you are not using a local web server to run you website.