Situation:
I am trying to include a partial with ng-include
without the need for any routing. I just want to include a specific partial of many dynamically. This is more or less how it looks like:
<div ng-controller="SomeController">
//This controller defines a $scope.getPartial(id)
<ng-include src="getPartial(something.id)"></ng-include>
</div>
It works, the partial is included. But looking at the console I can see that the controller is called several times and the first time it is called, I get a 404
GET path/to/partials/undefined.html [HTTP/1.1 404 Not Found 162ms]
it seems that something.id is not defined, when the include is made for the first time.
Questions:
I'm fairly new to AngularJS and may therefore make wrong assumptions about stuff or miss obvious things, please enlighten me.
The ng-include directive includes HTML from an external file. The included content will be included as childnodes of the specified element. The value of the ng-include attribute can also be an expression, returning a filename. By default, the included file must be located on the same domain as the document.
The ng-include directive is executed at priority level -400 and creates new scope every time it is invoked.
The primary purpose of the ng-include directive is used to fetch, compile, and include an external HTML file in the main AngularJS application. These are added as child nodes in the main application. The ng-include attribute's value can also be an expression, that returns a filename.
Definition and Usage. The ng-controller directive adds a controller to your application. In the controller you can write code, and make functions and variables, which will be parts of an object, available inside the current HTML element. In AngularJS this object is called a scope.
SomeController
, so you shouldn't have any problems with new scope.
$scope.getPartial = function(id){
if(!id){
return "";
}
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With