Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 角度指令。我可以取消优先级较低的指令吗_Angularjs_Angularjs Directive - Fatal编程技术网

Angularjs 角度指令。我可以取消优先级较低的指令吗

Angularjs 角度指令。我可以取消优先级较低的指令吗,angularjs,angularjs-directive,Angularjs,Angularjs Directive,假设我有一个元素,其中有几个指令。 例如: <div myFirstDirective='someFn' mySecondDirective></div> (myFirstDirective的优先级高于mySecondDirective) 因此,在本例中,如果myFirstDirective从函数返回一个假值,我甚至不希望mySecondDirective“运行”。 这可能吗 如果是,我将如何“取消”第二条指令 感谢您的帮助您可以在mySecondDirectiv

假设我有一个元素,其中有几个指令。 例如:

<div myFirstDirective='someFn'  mySecondDirective></div>

(myFirstDirective的优先级高于mySecondDirective)

因此,在本例中,如果myFirstDirective从函数返回一个假值,我甚至不希望mySecondDirective“运行”。 这可能吗

如果是,我将如何“取消”第二条指令


感谢您的帮助

您可以在mySecondDirective链接函数中计算myFirstDirective属性的结果。如果未应用正确的值,则立即退出链接功能

link: function(scope, element, attributes) {
    //link of mySecondDirective
    var firstValue = scope.$eval(attributes.myFirstDirective);
    if (!firstValue) return;

    // else do some cool stuff
}

你需要他们通过打电话给对方的控制器来互相交谈

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.
         }

     }
  }
});

哇,这两个都是聪明的解决方案,谢谢!理想情况下,我不希望第二个指令具有任何“依赖性”或了解第一个指令,但这为我打开了更多的思路:)您接受的答案也依赖于指令。我想知道是否还有另一种方法可以创建一个同时插入两个指令控制器的服务-然后您可以在主页控制器中调用该服务,并确定要呈现哪个指令。不幸的是,我没有一个代码示例。。。