如何从ng-repeat所在的同一元素上的另一个指令访问ng-repeat的范围?

时间:2022-01-31 23:44:18

I want to be able to access a property from the ng-repeat scope in another directive of the same element where the ng-repeat directive resides. For example, I'd like to have access to the child.class property in the following example:

我希望能够在ng-repeat指令所在的同一元素的另一个指令中从ng-repeat范围访问属性。例如,我想在以下示例中访问child.class属性:

<div ng-class="{{ child.class }}" ng-repeat="child in parent.children">
    {{ child.name }}
</div>

Here is a JSFiddle showing that this doesn't work.

这是一个JSFiddle,显示这不起作用。

If this isn't possible, what is the next best way to go about setting class on the element that has the ng-repeat? Is there a way to ng-repeat in a controller?

如果这是不可能的,那么在具有ng-repeat的元素上设置类的下一个最佳方法是什么?有没有办法在控制器中重复ng?

1 个解决方案

#1


1  

ng-class takes an expression and evaluates it. If you change it to:

ng-class接受表达式并对其进行求值。如果您将其更改为:

ng-class="child.class"

then it works! Here is a working JSFiddle.

然后它的作品!这是一个有效的JSFiddle。

Also just to mention, you can put in something like this:

还要提一下,你可以输入这样的东西:

ng-class="{'blue': true, 'red': false}"

true or false can be other variables or functions, but it only adds the classes with a value of true!

true或false可以是其他变量或函数,但它只添加值为true的类!

ng-class documentation

#1


1  

ng-class takes an expression and evaluates it. If you change it to:

ng-class接受表达式并对其进行求值。如果您将其更改为:

ng-class="child.class"

then it works! Here is a working JSFiddle.

然后它的作品!这是一个有效的JSFiddle。

Also just to mention, you can put in something like this:

还要提一下,你可以输入这样的东西:

ng-class="{'blue': true, 'red': false}"

true or false can be other variables or functions, but it only adds the classes with a value of true!

true或false可以是其他变量或函数,但它只添加值为true的类!

ng-class documentation