0

我正在创建一个指令,如果不满足某些条件,它将限制 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
    }
  });
4

2 回答 2

0

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

于 2016-01-21T16:46:13.513 回答
0

ngIf 将帮助您:

  <div ng-if="condition">
    <inner-element>
    </inner-element>
  </div>
于 2016-01-21T15:22:30.363 回答