angularjs - Angular 指令。我可以取消优先级较低的指令吗

标签 angularjs angularjs-directive

假设我有一个带有几个指令的元素。 例如:

<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/

相关文章:

javascript - 如何创建延迟的悬停 Action ,如果用户在时间到之前离开则不会执行? AngularJS

javascript - 使用 AngularJS 保存修改后的 json 文件

javascript - 有没有办法在 ui-view 之外重新加载 angular.js 指令?

AngularJS 将指令模板中的参数传递给 Controller

javascript - Angularjs dom加载准备就绪

HTML,Body height 100% 不起作用

javascript - 手机上的状态提供商

javascript - 我可以将独立作用域和继承作用域结合起来吗?

javascript - 如何动态地将动态指令添加到我的 Angular 应用程序中?

javascript - AngularJS:指令和 View 更新之间的通信