how to have unique scope in ng-repeat in angularjs

天涯浪子 提交于 2020-01-06 17:08:06

问题


Is it possible for me to have multiple array for $scope?

i have a list of div with child scopes that is generated from a parent scope in ng-repeat. How can i have the scope variable individually unique?

I am generating a list of ng-repeat in another ng-repeat.

<div ng-repeat="" ng-init="hide=true" ng-click="hide=!hide">
     <div ng-hide="hide" ng-init="childhide=true" ng-click="childhide=!childhide">
          <div ng-repeat="" ng-init="childhide" ng-hide="childhide">
               <div>{{ variable }}</div>
          </div>
     </div>
</div>

How can i have the variable unique? Coz each time when i click on either one div, all div with childhide variable will show. Anyway to make them behave individually?

Thanks.


回答1:


To get a new $scope for each div, the first ting that comes to mind is to create another directive and specify which type of scope you want.

<div class="container">
    <div ng-repeat="item in items"></div>
</div>

will become:

<div class="container">
    <inner-directive ng-repeat="item in items"></inner-directive>
</div>

then in inner-directive:

app.directive('innerDirective', function () {
    return {
        restrict: 'E',
        template: '<div></div>', // this replaces what you had before
        scope: {}
    };
});

This will create an isolate scope, which does not inherit properties from it's parent.

There are a couple of other scope options but i cant remember off the top of my head what each one does. Easy to read in the docs though.



来源:https://stackoverflow.com/questions/30361765/how-to-have-unique-scope-in-ng-repeat-in-angularjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!