Different ng-include's on the same page: how to send different variables to each?

前端 未结 15 1273
北海茫月
北海茫月 2020-12-04 12:17

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:

相关标签:
15条回答
  • 2020-12-04 13:08

    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

    0 讨论(0)
  • 2020-12-04 13:10

    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.

    0 讨论(0)
  • 2020-12-04 13:10

    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.

    0 讨论(0)
提交回复
热议问题