gpt4 book ai didi

javascript - 使用指令在 angularjs 中绑定(bind)自定义事件

转载 作者:行者123 更新时间:2023-11-29 21:42:36 25 4
gpt4 key购买 nike

我正在尝试通过包装 jquery 自动完成插件来创建一个指令像这样的东西

<input class="form-control" auto-complete ui-items="list" modvar="selectedSvr" callback="myfunction"/>

我想调用传递给回调属性的任何函数,我该如何实现?

这是我的指令

app.directive('autoComplete', function() {
return function($scope, iElement, iAttrs) {
iElement.autocomplete({
source: $scope[iAttrs.uiItems],
select: function (event,ui) {
$scope.$apply(function () {
$scope[iAttrs.modvar] = ui.item.value;
// maybe register/call myfunction here
})
}
});
};
});

最佳答案

我找到了解决方案 here .

现在代码看起来像

HTML

<input class="form-control" auto-complete ui-items="searchList" modvar="selectedItem" on-callback="callme()"/>

Angular

app.directive('autoComplete', function() {
return function ($scope, iElement, iAttrs) {
iElement.autocomplete({
source: $scope[iAttrs.uiItems],
select: function (event,ui) {
$scope.$apply(function () {
$scope[iAttrs.modvar] = ui.item.value;
$scope.$eval(iAttrs.onCallback);
})
}
});
};
});

关于javascript - 使用指令在 angularjs 中绑定(bind)自定义事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32252179/

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