我在 AngularJS 中创建了一个自定义指令。在链接函数中,我将 ng-model 和 ng-options 属性添加到内部模板,但不幸的是,绑定不起作用。但是当我将内部模板原样放入我的 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;
});