假设我有一个带有几个指令的元素。 例如:
<div myFirstDirective='someFn' mySecondDirective></div>
(myFirstDirective 的优先级高于 mySecondDirective)
因此,在这种情况下,如果 myFirstDirective 从函数返回 false 值,我什至不希望 mySecondDirective “运行”。 这可能吗?
如果是这样,我如何“取消”第二个指令?
感谢您的见解
最佳答案
您需要他们通过调用对方的 Controller 来相互交谈。
app.directive('myFirstDirective', function() {
return {
restrict: 'A',
priority: 20,
controller: function ($scope) {
var result = false;
if ($scope.myFirstDirective) {
result = $scope.myFirstDirective();
}
this.shouldRun = function() {
return result;
}
},
scope: { myFirstDirective: '&' }
}
});
app.directive('mySecondDirective', function() {
return {
restrict: 'A',
priority: 10,
require: '?myFirstDirective',
link: function(scope, element, attr, otherController) {
if(otherController && otherController.shouldRun()) {
// execute this directive only if
// function from first directive returns true.
}
}
}
});
关于angularjs - Angular 指令。我可以取消优先级较低的指令吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25897829/