简体   繁体   English

角ng-repeat导致隔离范围

[英]Angular ng-repeat causes isolated scope

I could use help with understanding how to make these 2 examples work in the same way. 我可以使用帮助来理解如何使这两个示例以相同的方式工作。 For starters, here's my full example: http://jsfiddle.net/tylerbrinks/DZbfD/ 首先,这是我的完整示例: http : //jsfiddle.net/tylerbrinks/DZbfD/

I'm using a simple controller to manage index. 我正在使用一个简单的控制器来管理索引。 I realize it's not 100% necessary, but I'm simplifying a much larger problem down to it's most basic parts for help. 我意识到这不是100%必要的,但我正在将更大的问题简化为最基本的帮助部分。 The controller looks like this: 控制器如下所示:

function TabCtrl($scope){
    $scope.index = 0;
}

I'm using the index value to maintain which item in a list is selected (like tabs). 我正在使用索引值来维护列表中选中的项目(如标签)。

When elements are already in the DOM, they behave exactly as I'd expect. 当元素已经在DOM中时,它们的行为将完全符合我的预期。 Clicking each item toggles which item is selected. 单击每个项目可切换选择哪个项目。 In the JsFiddle, the color changes to show the selected item. 在JsFiddle中,颜色会更改以显示所选项目。

<div ng-controller="TabCtrl">
    <div ng-class="{selected: index == 0}" ng-click="index = 0">Click Me. Position is 0, scope.index is {{index}}</div>
    <div ng-class="{selected: index == 1}" ng-click="index = 1">Click Me. Position is 1, scope.index is {{index}}</div>
    <div ng-class="{selected: index == 2}" ng-click="index = 2">Click Me. Position is 2, scope.index is {{index}}</div>
</div>

That's perfect - behaves as I'd like. 太好了-表现如我所愿。 My problem is that I have a dynamic list and I'm using ng-repeat to build it out. 我的问题是我有一个动态列表,并且正在使用ng-repeat进行构建。 When I do, ng-repeat seems to give a new scope to each item in the list. 当我这样做时, ng-repeat似乎为列表中的每个项目赋予了新的作用域。 That means I can't toggle between them because they don't share the same index property. 这意味着我无法在它们之间切换,因为它们不共享相同的索引属性。 Instead, each list item has it's own scope and each scope has a unique index value. 而是,每个列表项都有自己的范围,并且每个范围都有唯一的索引值。

Here's the same thing, but using ng-repeat. 这是同一件事,但是使用ng-repeat。 Instead of hard coding the index on click, I'm setting it to the $index property of the repeater. 我将其设置为转发器的$ index属性,而不是对点击时的索引进行硬编码。

<div ng-controller="TabCtrl">
    <div  ng-repeat="item in [1,2,3]" ng-class="{selected: index == $index}" ng-click="index = $index">Click Me.  Position is: {{$index}}, scope.index is {{index}}</div> 
</div>

How can I make the dynamic list share a single scope like the static list? 如何使动态列表与静态列表共享单个作用域?

Thanks for the help! 谢谢您的帮助!

You can create an object in your parent scope so that you can use the value in your child scopes. 您可以在父范围中创建一个对象,以便可以在子范围中使用该值。 Something like this: 像这样:

Fiddle 小提琴

function TabCtrl($scope){
    $scope.myVariables = { index : 0 };
}

<div ng-controller="TabCtrl">
    <div ng-repeat="item in [1,2,3]" ng-class="{selected: myVariables.index == $index}" ng-click="myVariables.index = $index">Click Me.  Position is: {{$index}}, myVariables.index is {{myVariables.index}}</div> 
</div>

Because a new scope is created for each item in the list, your assignment to index will create a new variable in each new scope; 因为为列表中的每个项目创建了一个新的作用域,所以您对索引的赋值将在每个新的作用域中创建一个新变量。 essentially, each item in the list has its own copy of the index variable. 本质上,列表中的每个项目都有其自己的index变量副本。

Instead, declare your index as a sub-field of the parent scope: 而是将索引声明为父范围的子字段:

function TabCtrl($scope){
    $scope.container = {
      index: 0
    }
}

And then refer to container.index in your HTML in all the places where you used to refer to index. 然后在您以前引用索引的所有位置引用HTML中的container.index。

I use the rule that all HTML references to scope variables should include the '.' 我使用以下规则:所有对范围变量的HTML引用都应包含“。” - if they don't then I've probably broken a scoping rule. -如果他们不这样做,那么我可能已经违反了范围界定规则。

Yes you have to have something in your parent scope as others I've said. 是的,您必须像我已经说过的那样在您的父范围中拥有一些东西。 I personally like to use a function 我个人喜欢使用功能

so in your Controller: 所以在您的控制器中:

  $scope.activate= function(index){
      $scope.index=index;
  };

and then ng-class: 然后ng-class:

ng-class="{ active:index=={{$index}}}"

and ng-click: 并单击ng:

ng-click="activate({{$index}})"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM