gpt4 book ai didi

javascript - 与 AngularJS 的指令绑定(bind)

转载 作者:行者123 更新时间:2023-11-30 13:04:25 25 4
gpt4 key购买 nike

我在 AngularJS 中创建了一个自定义指令。在链接函数中,我将 ng-model 和 ng-options 属性添加到内部模板,但不幸的是绑定(bind)不起作用。但是当我将内部模板按原样放入我的 html 文件时,一切正常。

application.directive("customSelect", function () {
var directive = {
restrict: 'E',
template: '<select name="ArrDeplocation" class="arrdepSelect"></select>',
link: function (scope, elem, attr) {
console.log('scope: ', scope);
console.log('element: ', elem);
console.log('attribute: ', attr);

$(elem.children()[0]).attr('ng-model', 'model.selectedCity');
$(elem.children()[0]).attr('ng-options', 'city for city in model.cities');

$(elem.children()[0]).selectmenu();

}
};
return directive;
});

最佳答案

我不明白为什么需要在链接函数中设置属性。您可以简单地放入您的模板。

http://plnkr.co/edit/9u6nkLYKHxBBiJ60mpbF?p=preview

app.directive("customSelect", function () {
var directive = {
restrict: 'E',
template: '<select name="ArrDeplocation" class="arrdepSelect"'
+ 'ng-model="model.selectedCity" ng-options="city for city in model.cities"> </select>',
link: function (scope, elem, attr) {
// run jquery mobile methods here...
}
};
return directive;
});

您可能想在这里详细说明您真正想要实现的目标。

关于javascript - 与 AngularJS 的指令绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16208627/

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