克隆在执行任何子指令之前转义内容

时间:2014-11-04 20:27:44

标签: angularjs angularjs-directive angularjs-ng-transclude

我创建了一个指令,用于显示另一个指令的示例,该指令作为bootstrap-like documentation page的一部分使用。这是一个例子:

<an-example>
  <input ng-model="name" an-spellcheck />
</an-example>

我希望它复制放在其中的标记,并将其放入<code>标记中,以显示使用该指令所需的标记。

ng-model为输入添加了类,使其成为:

<input class="ng-scope ng-pristine" ng-model="name" an-spellcheck />

相反,example指令应该捕获:

<input ng-model="name" an-spellcheck />

但是,在任何内部指令运行之前,我似乎无法访问被转换的内容。这是a plunker demonstrating the issue

controllerpreLinkpostLink函数中,输入已包含来自ng-model的css类。

在angular编译子指令之前,有没有办法访问example指令的内容?

1 个答案:

答案 0 :(得分:0)

这是不可能的,因为ngTransclude的优先级为0,而ngModel的优先级为1(编译顺序是从较高的数字到较低的数字)。 因此,将始终首先编译ngModel。可以更改指令的优先级,以便在ngModel之前进行编译,但转换将在以后应用

ngModel优先级: https://docs.angularjs.org/api/ng/directive/ngModel

ngTransclude优先级: https://docs.angularjs.org/api/ng/directive/ngTransclude