我正在编写一个自定义指令来生成一个下拉元素。如果我使用隔离范围,编译函数不会转换模板。
大多数情况下,我正在更改 select 元素上的 ng-options,因为它们是在指令中提供的。我如何在隔离范围内实现相同的目标?
myApp.directive('helloWorld', function () {
return {
restrict: 'E',
replace: true,
scope: {
id:'@',
label:'@'
},
template: '<div class="control-group">' +
' <label for="{{id}}" class="control-label">{{label}}</label>' +
' <div class="controls">' +
' <select id="{{id}}" class="medium m-wrap">' +
' </select>' +
' </div>' +
'</div>',
},
compile:function(tElement, tAttrs, transclude){
var opts = tAttrs.textField
?'item.' + tAttrs.textField + (tAttrs.groupBy ? ' group by item.' + tAttrs.groupBy : '') + ' for item in ' + tAttrs.itemSource
:'item for item in ' + tAttrs.itemSource;
tElement.find('select').attr('ng-options',opts);
}
});