如何编写需要 ng-model
或 k-ng-model
的指令?文档不包括这个:(
app.directive('myDir', function () {
return {
require: 'ngModel || kNgModel',
// omitted
};
});
最佳答案
您需要将它们作为字符串数组传入。
您无法告诉 Angular 至少其中一个需要可用,因此将它们设置为可选,并检查链接函数是否两者之一可用。将您的代码更新为:
app.directive('myDir', function () {
return {
require: ['?ngModel', '?kNgModel'],
link: function(scope, element, attrs, controllers){
if(!controllers[0] && !controllers[1]){
throw 'myDir expects either ng-model or k-ng-model to be defined';
}
}
};
});
关于angularjs - 需要两个属性之一的 Angular 指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31830917/