Hussain Mirahmadi Hussain Mirahmadi - 3 months ago 23
AngularJS Question

Why is AngularJS include not working in this simple example?

This is the content of main page:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
</head>
<body>
<h1>Hello</h1>
<div ng-include="'goodbye.html'" ></div>
</body>
</html>





This is the content of goodbye.html :

<h1>Goodbye</h1>





It just shows the "Hello" and doesn't add the "Goodbye" to that. Why doesn't it include the goodbye.html?

Answer

You are missing ng-app to bootstrap AngularJS.

Also, ng-include evaluate the expression passed so you need to pass a string 'goodbye.html', not goodbye.html

<html ng-app="example">
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
        <script type="text/javascript">
            angular.module('example',[]);
        </script>
    </head>
    <body>
        <h1>Hello</h1>
        <div ng-include="'goodbye.html'"></div>
    </body>
</html>
Comments