如何在angularjs的ng-repeat中拥有唯一的范围 [英] how to have unique scope in ng-repeat in angularjs

查看:26
本文介绍了如何在angularjs的ng-repeat中拥有唯一的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以为 $scope 设置多个数组吗?

我有一个带有子作用域的 div 列表,它是从 ng-repeat 中的父作用域生成的.如何让范围变量单独唯一?

我正在另一个 ng-repeat 中生成一个 ng-repeat 列表.

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

如何让变量唯一?因为每次我点击任一 div 时,所有带有 childhide 变量的 div 都会显示.无论如何要让他们单独表现?

谢谢.

解决方案

要为每个 div 获得一个新的 $scope,首先想到的是创建另一个指令并指定您想要的作用域类型.

>

<div ng-repeat="项目中的项目"></div>

将变成:

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

然后在内部指令中:

app.directive('innerDirective', function () {返回 {限制:'E',template: '<div></div>',//这个替换了你之前的范围: {}};});

这将创建一个隔离作用域,它不会从其父级继承属性.

还有其他几个范围选项,但我无法记住每个选项的作用.不过在文档中易于阅读.

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.

解决方案

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.

这篇关于如何在angularjs的ng-repeat中拥有唯一的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆