gpt4 book ai didi

javascript - 在 Angularjs 中使用指令触发 ng-change

转载 作者:行者123 更新时间:2023-12-03 08:42:22 24 4
gpt4 key购买 nike

我正在使用 ng-options 来迭代 angularjs 中的一些数据,当选择值时我想要一个选定的对象,为此我创建了指令..

return {
require: 'ngModel',
restrict : 'A',
link: function(scope, element, attrs, ctrl) {
$(element).combobox({
source: scope[attrs.reportitems],
select: function(event, ui) {
$timeout(function() {
if(attrs.reportitems=="zoneList"){
if(ui.item.attributes.value.value != "0") {
scope["update"](ui.item.attributes.value.value);
}
}
}, 0);
}
});
},
}

这将调用 Controller 中的更新方法,并将传递数据数组的索引位置,但我想要对象本身而不是索引。或者以某种方式触发 UI 中的 ng-change 方法。这样我就可以在 Controller 中的 update 方法中拥有一个对象。请检查这个 Plunker 的完整代码.. Link

有人可以帮忙吗?

最佳答案

据我所知,当选择一个项目时,整个对象都会在数组内部传递。您只需要 res[0] 即可访问该对象。从那里您可以使用 id、tableName 和 toolTip 属性。或者,您可以只分配 ng-change=update(selected[0]) 来获取对象。

我很好奇为什么你定义 $scope.selected = [] 然后分配 $scope.selected = res

关于javascript - 在 Angularjs 中使用指令触发 ng-change,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33010152/

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