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

前端 未结 15 1272
北海茫月
北海茫月 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:01

    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'.

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

    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.

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

    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.

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

    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/

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

    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

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

    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.

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