如何从外部指令停止嵌套指令执行?

时间:2016-01-21 14:55:19

标签: javascript angularjs angularjs-directive angularjs-compile

我正在创建一个指令,如果不满足某些条件,它将限制HTML访问。遗憾的是,在更改和编译内部HTML之前调用内部指令时会出现问题。

是否可以从任何外部指令停止嵌套指令执行?

示例(请参阅控制台):http://jsfiddle.net/xaQzb/14/

HTML:

<div ng-app="testApp">
  <outer-element>
    <inner-element>
    </inner-element>
  </outer-element>
</div>

应用

angular.module('testApp', []);

var app = angular.module('testApp');

app
  .directive('outerElement', ['$compile', function($compile) {

    function linker(scope, element) {
      element[0].innerHTML = 'Replaced HTML and should not execute anything inside';
      $compile(element.contents())(scope);
    }

    return {
      restrict: 'E',
      scope: false,
      link: linker
    }
  }]);

app
  .directive('innerElement', function() {

    function linker(scope, element) {
      console.log('Directive has been called');
    }

    return {
      restrict: 'E',
      scope: false,
      link: linker
    }
  });

2 个答案:

答案 0 :(得分:0)

ngIf会帮助你:

  <div ng-if="condition">
    <inner-element>
    </inner-element>
  </div>

答案 1 :(得分:0)

您正在寻找terminal: true指令选项。