我想在我的指令编译时添加此选项。
我一直在尝试这个:
HTML
<input ng-model="myValue" my-directive>
JS
angular
.module('myApp')
.directive('myDirective', directive);
directive.$inject = ['$compile'];
function directive($compile){
var ddo = {
restrict: 'A',
require: 'ngModel',
link: {pre: prelinkFn}
};
return ddo;
function prelinkFn(scope, element, attrs){
attr['ngModelOptions'] = { updateOn: 'blur' };
//I think that in this function, when i compile the element, the attributes
//are compiled too, but this is not working
$compile(element)(scope);
}
}
我的问题:
答案 0 :(得分:3)
您应该使用ngModelCtrl.$options
:
app.directive("myDirective", function($compile){
return {
require: 'ngModel',
link: function(scope, iElem, iAttrs, ngModelCtrl) {
ngModelCtrl.$options = {
updateOn: 'blur',
updateOnDefault: true,
debounce: {
'blur': 1000,
'default': 1000
}
}
// Then do what you want... :)
}
}
});