I’ve got a page in my AngularJS app in which I would like to include the same html partial, but with different variables. If I do this in my main html:
<div id="div1" ng-include src="partials/toBeIncluded.html onload="var='A'">
<div id="div2" ng-include src="partials/toBeIncluded.html onload="var='B'">
And toBeIncluded.html looks like
<div>{{var}}</div>
Both div‘s will look like
<div id="div1"><div>B</div></div>
<div id="div2"><div>B</div></div>
I guess it has to do with the fact that the same onload gets called for al the ng-includes. So how do I send different variables to each different include?
The expression passed to
onloadevaluates every time a new partial is loaded. In this case you are changing the values ofvartwice so by the time both partials are loaded the current value will beBYou want to pass different data to each partial/template (with the underlying html file being the same). To achieve this, as Tiago mentions, you could do it with different controllers. For example, consider the following
Here, we have two partials, each with its own scope managed from their own controller (
ctrlAandctrlB), both children scopes ofMainCtrl. The functionhi()belongs to the scope ofMainCtrland will be run twice.If we have the following controllers
And the contents of
toBeIncluded.htmlareThe resulting html would be something along the following lines
and
Example here: http://plnkr.co/edit/xeloFM?p=preview