gpt4 book ai didi

javascript - 如何将任何数组从 ng-repeat 传递到 AngularJs 属性指令?

转载 作者:行者123 更新时间:2023-12-03 07:25:21 25 4
gpt4 key购买 nike

我有一个菜单项层次结构列表,其中每个项目都有一个子列表。对于 menuItems 属性来自 Controller 范围(父级列表)。我想传递一个数组以供以下示例的两个级别上的指令使用。

HTML代码

<div sortable="menuItems">
<div ng-repeat="item in menuItems">
...
<div sortable="item.Children">
<div ng-repeat="child in menuItems.Children">...</div>
</div>
</div>
</div>

指令代码

app.directive('sortable', function ($timeout) {
return {
restrict: 'A',
link: function($scope, $elem, attrs) {
// I need access by the array in here.
}
};
});

我已将可排序属性设置为列表名称的值,这显然不适合我。

最佳答案

您可以设置范围监视(或集合监视),因为属性的值是范围表达式。

app.directive('sortable', function ($timeout) {
return {
restrict: 'A',
link: function($scope, $elem, attrs) {
$scope.$watchCollection(attrs.sortable, function (newCollection, oldCollection) {
// (do something with newCollection)
});
}
};
});

关于javascript - 如何将任何数组从 ng-repeat 传递到 AngularJs 属性指令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36025677/

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