What is the correct syntax of ng-include?
You have to single quote your src
string inside of the double quotes:
<div ng-include src="'views/sidepanel.html'"></div>
Source
How the right way to use ng-include
Are you accessing the app through a web server or by accessing the file through file://
protocol? In the latter case it won't work due to browser's security restrictions. The solution would be to install Apache or Nginx and access the app through something like http://localhost/myapp/index.html
.
Angular ng-include and specifying controllers
mainCtrl and it's scope will be also available in your included part.
Calling parent function in ng-include
I can't debate on this point, but if you div has nested in several controller then you need to use $parent.$parent.$parent
, depending on controller hierarchy it could be increased in length.
So I'd suggest you to use controllerAs syntax which provide alias of controller & it can be accessible inside any div by using alias of any controller you can access its method.
Markup
<div ng-controller="MainCtrl as main">
{{ main.title }}
<div ng-controller="AnotherCtrl as another">
{{ another.title }}
<div ng-controller="YetAnotherCtrl as yet">
{{ yet.title }}
</div>
</div>
</div>
Refer this article for more detailed info.
AngularJS ng-include doesn't work
You cannot import local files directly using the file:// protocol.
You should take a look at:
Is it possible to use ng-include without web server?
Is there any limitation to use ng-include
The major problem is that each ng-include generates a http request, which will impact the page load times.
If all your doing is splitting up a page, you have a couple of options:
- Create directives, if your HTML can be used in others areas of your app.
- Use a templating engine like handlebars and a preprocessor to combine all the partials, in to a single page, like GulpJS or Grunt (i prefer Gulp).
Related Topics
How to Scroll to Top of Page With JavaScript/Jquery
How to Replace Text Inside a Div Element
Html ≪Select≫ Selected Option Background-Color CSS Style
JavaScript - Shuffle HTML List Element Order
Html5 Canvas Drawimage Ratio Bug Ios
How to Force the Browser to Reload Cached CSS and JavaScript Files
Instead of Using Prefixes I Want to Ask Site Visitors to Upgrade Their Browser
Setting Vendor-Prefixed CSS Using JavaScript
How to Remove CSS Property Using JavaScript
How to Format a Date in JavaScript
Sorting Options Elements Alphabetically Using Jquery
Uncaught Error: Security_Err: Dom Exception 18 When I Try to Set a Cookie
Want to Add "Addeventlistener" on Multiple Elements With Same Class
How to Use External ".Js" Files
Positioning ≪Div≫ Element At Center of Screen
Get a CSS Value With JavaScript