42

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?

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
Jasper Schulte
  • 7,181
  • 7
  • 23
  • 19
  • A solution is create a new directive, as i said in this answer: http://stackoverflow.com/a/36916276/2516399 – smartmouse Apr 28 '16 at 13:34

15 Answers15

40

The expression passed to onload evaluates every time a new partial is loaded. In this case you are changing the values of var twice so by the time both partials are loaded the current value will be B

You 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

<body ng-controller='MainCtrl'>    
  <div ng-include src='"toBeIncluded.html"' ng-controller='ctrlA' onload="hi()"></div>
  <div ng-include src='"toBeIncluded.html"' ng-controller='ctrlB' onload="hi()"></div>
</body>

Here, we have two partials, each with its own scope managed from their own controller (ctrlA and ctrlB), both children scopes of MainCtrl. The function hi() belongs to the scope of MainCtrl and will be run twice.

If we have the following controllers

app.controller('MainCtrl', function($scope) {
  $scope.msg = "Hello from main controller";
  $scope.hi= function(){console.log('hi');};
});

app.controller('ctrlA', function($scope) {
  $scope.v = "Hello from controller A";
});

app.controller('ctrlB', function($scope) {
  $scope.v = "Hello from controller B";
});

And the contents of toBeIncluded.html are

<p>value of msg = {{msg}}</p>
<p>value of v = {{v}} </p>

The resulting html would be something along the following lines

<p>value of msg = Hello from main controller</p>
<p>value of v = Hello from main controller A </p>

and

<p>value of msg = Hello from main controller</p>
<p>value of v = Hello from controller B </p>

Example here: http://plnkr.co/edit/xeloFM?p=preview

jaime
  • 41,961
  • 10
  • 82
  • 52
  • 6
    Ok, this seems clear, thanks. But to elaborate on on my problem: I want te create an `ng-repeat` and within this repeat, I would like to include a partial, everytime with a different value for a given variable. This will not be possible (at least not with the above solution) because you would neet to dynamically generate new controllers to generate new scopes. I'm I right assuming this? – Jasper Schulte Dec 11 '12 at 12:58
  • why do the include need single and double quotes? eg. g-include src='"toBeIncluded.html"'? Thanks! – Karan Aug 14 '13 at 19:54
  • Because if it's not a string, angular evaluates it instead of loading it directly. See here http://stackoverflow.com/questions/13943471/angularjs-ng-include – Joren Jan 09 '14 at 03:36
  • Is the `ng-init` also usable? – Fractaliste Apr 09 '14 at 07:36
27

Just like what Mark said, but to simplify it a little bit and make it more "on-the fly" is to use the following:

<div ng-repeat="name in ['John']" ng-include="'name-template.html'"></div>
<div ng-repeat="name in ['Jack']" ng-include="'name-template.html'"></div>

<script type="text/ng-template" id="name-template.html">
   <div>The name is {{ name }}</div>
<script>

Example here: http://jsfiddle.net/Cndc6/4/

Denis Pshenov
  • 11,157
  • 6
  • 42
  • 42
20

In your comment on @jm-'s answer, you mention you want to load your partial inside ng-repeat. To do this, create an array on your $scope, and set the unique values on that array:

$scope.partialStuff = [ 'p1', 'p2', 'p3' ];

Somewhere in your partial:

{{partialVar}}

The HTML:

<div ng-repeat="partialVar in partialStuff">
   <div ng-include src="'partials/toBeIncluded.html'"></div>
</div>

Fiddle.

Steven Almeroth
  • 7,758
  • 2
  • 50
  • 57
Mark Rajcok
  • 362,217
  • 114
  • 495
  • 492
10

I could be wrong but wouldn't it be better to use a Directive?

.directive('foobar', [function factory() {
    return {
        restrict: 'E',
        replace: true,
        templateUrl: '/toBeIncluded.html',
        scope: {
            "var": "="
        }
    };
}]);

And your HTML

<div ng-repeat="var in data">
    <foobar var="var"></foobar>
</div>

Now the foobar element should be replaced with your template. Each having it's own scope.

Dave Kok
  • 892
  • 9
  • 19
  • 2
    BTW, you have three ways to bind an attribute in the scope option. Using =, @ and &. If you use @ it will be used literally. With = the parent scope is searched for a variable of the same name and it's value is used. With & the attribute value is evaluated as an expression. – Dave Kok Mar 13 '14 at 08:51
  • 1
    This does not work for me. Inside the template, if I type `{{var}}` I get the object with all its data, however doing `{{var.foo}}` produces nothing. – chovy Jun 06 '14 at 08:26
  • i had to set `'='` for an object, `@` is just a string. – chovy Jun 06 '14 at 09:01
1

Specifically, ng-init and onload expressions are evaluated in the parent scope, not the child scope created by the nginclude. Thus you are assigning values to var on the parent scope twice, both of which are happening before the content inside those ng-includes is loaded compiled and evaluated. When each child scope is created it inherits from the parent scope, where var='B'.

autoric
  • 11
  • 1
1

consider you have variable foo inside your partial,

then you will pass foo like this:

<ng-include src="'partials/foo-part.html'" onLoad="foo='bar'"></ng-include>

for more browser compatibility you can pass it like this:

<div ng-include src="'partials/foo-part.html'" onLoad="foo='bar'"></div>

you can also Use onInclude="foo='bar'" instead of onLoad

plnkr.co/edit/GHRNyAMBKLvgtf3NFSRu?p=info

Hassan Faghihi
  • 1,888
  • 1
  • 37
  • 55
0

The same "onLoad" does not get called for all ng-includes. Most likely, var is set to A, and then to B. The thing is, it is being shared across both includes, because they are on the same scope. ´var´, as a model in angularJS, is scope-dependant.

In order for your partials to have separate values, they must each have their own scopes. The easiest way to do this would bet to set up a controller or directive and assign it to the Dom element where the ng-include is, and set your value within that scope.

Tiago Roldão
  • 10,629
  • 3
  • 29
  • 28
0

I had the very same problem and I found a way to achieve what I wanted of course it does not look very pretty but still.

I used the ng-repeat which will create a new scope to include the very same template twice with different values like so (dummy example):

<script type="text/ng-template" id="mod-edito.html">
<div class="mod-edito__media">
    <img ng-src="{{ edito.cover.src }}" />
</div>
</script>

<div>
    <ng-include ng-repeat="edito in [edito_object]" src="'mod-edito.html'"></ng-include>
    ...
    <ng-include ng-repeat="edito in [edito_other]" src="'mod-edito.html'"></ng-include>
</div>

Is that acceptable? I guess so.

jrmgx
  • 719
  • 8
  • 24
0

I was looking for an alternative, I wanted a js object to send a few parameters into the included partial. This is the evolved version of the one made by @denis-pshenov. You can use a controller to fill the data, or an ng-init.

Basically, something like this,

var myApp = angular.module('myApp',[]);

myApp.controller('Controller', function($scope) {
    $scope.ourData = [ { name : 'John', address : 'Paradise' } ];
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-controller="Controller">
    <div ng-repeat="data in ourData" ng-include="'partial.html'"></div>
    
    <div ng-init="ourData2 = [ { name : 'Jack', address : 'Paradise' } ]" />
    <div ng-repeat="data in ourData2" ng-include="'partial.html'"></div>
    
</div>

<script type="text/ng-template" id="partial.html">
   <div>The name is {{ data.name }} and the address is {{ data.address }} </div>
</script>
genuinefafa
  • 703
  • 1
  • 6
  • 21
0

There is a way to create a universal reusable directive.

Here is what a colleague of mine had come up with (inspired by Knockout.js).

Directive:

(function () {
    'use strict';

    angular
        .module('directives')
        .directive('ngWith', ngWith);

    function ngWith() {
        return {
            restrict: 'A',
            replace: true,
            scope: {
                model: '=',
                template: '@'
            },
            template: '<div data-ng-include="template"></div>',
        };
    }
})();

Usage:

<div data-ng-with="" data-model="parentObj.childObj.whatEver" data-template='my-template.html'></div>
Den
  • 1,827
  • 3
  • 25
  • 46
0

Just a heads up, if you include the partial within an ng-repeat, you can still have access to the parent scope's $index. Just include {{$index}} in the partial.

JMichaliga
  • 53
  • 1
  • 5
0

Using onload is not a clean solution because it litters the global scope. If you have something more complex, it'll start to fail.

Using a new directive instead of ng-include is a cleaner solution.

The ideal usage looks like:

<div ng-include-template="partials/toBeIncluded.html" ng-include-variables="{ var: 'A' }"></div>
<div ng-include-template="partials/toBeIncluded.html" ng-include-variables="{ var: 'B' }"></div>

The directive is:

.directive(
  'ngIncludeTemplate'
  () ->
    {
      templateUrl: (elem, attrs) -> attrs.ngIncludeTemplate
      restrict: 'A'
      scope: {
        'ngIncludeVariables': '&'
      }
      link: (scope, elem, attrs) ->
        vars = scope.ngIncludeVariables()
        for key, value of vars
          scope[key] = value
    }
)

You can see that the directive doesn't use the global scope. Instead, it reads the object from ng-include-variables and add those members to its own local scope.

ng-include is not that reusable because it has access to the global scope. It's a little weird.

I hope this is what you would like; it's clean and generic.

Tanin
  • 1,853
  • 1
  • 15
  • 20
0

You may do it refer to features single values like $scope.a = 'test' and for objects like $scope.a = {a.test}; and use different scope to define visiblity area.

From ng-if documentation.

Note that when an element is removed using ngIf its scope is destroyed and a new scope is created when the element is restored.

Simpe solution is to include ng-if and define you static conetent into ng-init directive.

<div ng-if="<any-true-expression>" ng-init="val='First'" ng-include="'html/common.html'"</div>

<div ng-if="<any-true-expression>" ng-init="val='Second'" ng-include="'html/common.html'"</div>

In this case it will be show with different values like

{{val}} shows >>
First
Second

This Content is static because val is simpe reference to value.

If you want to have dynamically changeable pages which depends on some common scope values you need to destroy and recreate scope that has been created when used directive ng-if. Like this:

<div id="wrapper" ng-if="orders_client">
       <div id="client-orders" ng-init="orders = orders_client" ng-include="'html/pages/parts/orders-view.html'"></div>
</div>

and

$scope.orders_client = null;
   /*recreate content table*/
     $timeout(function () {
        $rootScope.general.showPreloader = false;
        $scope.orders_client = $scope.baseData.clients[newClient];
     }, 500);

In this case scope with previously data will be destroyed and new scope with new data will be created. In need to time to recreate new scope 500ms. Please write if some better catch exist.

Arch
  • 517
  • 2
  • 7
  • 17
0

You can use ng-if=true to force a new scope each time you load a partial through ng-include.

Your code will look like

<div id="div1" ng-include src="partials/toBeIncluded.html onload="var='A'" ng-if="true">
<div id="div2" ng-include src="partials/toBeIncluded.html onload="var='B'" ng-if="true">

An example can be found at this plunker

Another answer referencing the same idea here

helcode
  • 1,859
  • 1
  • 13
  • 32
-3

Just put each ng-include under its own <div> as following:

<div>
  <div id="div1" ng-include src="partials/toBeIncluded.html onload="var='A'"/>
</div>
<div>
  <div id="div2" ng-include src="partials/toBeIncluded.html onload="var='B'"/>
</div>

Each included HTML will then have its own var in its own scope.

tphongio
  • 7
  • 1