我创建了一个简单的指令,称为 match
,它的用法如下:
<input match='pattern' />
我的指令的声明行是:
app.directive('match', function () {
return {
restrict: 'A',
require: 'ngModel',
scope: {
pattern: '=match'
},
link: function (scope, element, attributes, ngModel) {
// doing stuff here
}
};
});
然而,过了一段时间我想为 angularjs 使用 BootstrapUI,当我开始使用 typeahead 组件时,他们在使用相同范围时遇到了问题:
Multiple directives [match, uibTypeaheadMatch] asking for new/isolated scope on
我需要匹配
,并在一页中一起输入。 Typeahead 不在我的控制之下,我不想更改 match
的名称。
我能做些什么来防止它们发生碰撞?
最佳答案
问题是您的指令和 Typeahead 指令都要求在同一元素上使用隔离范围,而 Angular 不允许这样做。
要克服这个问题,请以不同的方式定义指令:
app.directive('match', function () {
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, element, attributes, ngModel) {
var match = attributes.match;
//do your stuff
}
};
});
关于javascript - 如何防止angularjs指令之间的范围冲突?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37811195/