gpt4 book ai didi

javascript - 可以访问 ng-repeat 创建的范围项吗?

转载 作者:数据小太阳 更新时间:2023-10-29 05:51:56 26 4
gpt4 key购买 nike

我的印象是 ng-repeat 为数组/对象中的每个元素创建了一个新的范围。

是否可以从 Controller 访问 ng-repeat 创建的这些新范围?例如,如果您知道索引?

如有任何帮助,我们将不胜感激。

最佳答案

检查此演示的控制台:JSFiddle .

console.log作用域,有两个属性$$childHead$$childTail .它们是 ng-repeat 创建的第一个也是最后一个子作用域。 .

获得第一个子作用域后$$childHead ,可以遍历得到其他ng-repeat通过 $$nextSibling 范围对象和 $$prevSibling .

Note: these attributes start with $$, which indicate that they are private Angular variables. I suppose they are not intended to be used directly.

如果您使用 ng-repeat喜欢<div ng-repeat="item in items"></div> , 你可以使用 ng-click="dealWithItem(item, $index)"将此项传递给 Controller ​​中定义的函数:

$scope.dealWithItem = function (item, index) {
// console.log(item);
}

它适用于嵌套 ng-repeat还有:JSFiddle .

关于javascript - 可以访问 ng-repeat 创建的范围项吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31931589/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com