2017-01-20 19 views
0

我在角度管理伪指令作为属性时遇到了麻烦。使用伪指令作为属性时的截断

我有使用transclusions元素指令:

<xx-button>Button!</xx-button> 

而且我有加入一些其它指令,以它改变元素的内容的指令:

<xx-button xx-modifying-directive>Button!</xx-button> 

这将修改为:

<xx-button some-other-directives> 
    <button>Button!</button> 
</xx-button> 

当我编译li中元素的内容nking的xx-modifying-directive指令的功能,我得到以下错误:

Illegal use of ngTransclude directive in the template! No parent directive that requires a transclusion found. Element:

您可以在此JSFiddle更清楚地看到这个问题。

阅读文档或ngRepeat的源代码我看不到如何解决这个问题。

注意:我需要在xx-modifying-directive中编译我的元素,因为此指令添加了需要编译的其他指令(在我的实际案例中,我添加了像hm-pan这样的锤子处理程序)。

更新:

仔细看,问题来自于事实(ngTransclude指令)[https://github.com/angular/angular.js/blob/2a156c2d7ec825ff184480de9aac4b0d7fbd5275/src/ng/directive/ngTransclude.js#L59]没有通过transclude功能。

仍然不知道为什么,因为它实际上有一个启动transclude的祖先(xx-button)。

+0

您试图手动编译声明了属性指令的(父)元素,这是不正确的,实际上并没有实现任何内容。编译服务应该与一个新的模板字符串一起使用,这是您动态生成的。请仔细阅读服务文档,这可能是最复杂,最难理解的角色主题之一。 –

+0

我相信'元素'代表,并且随着指令被添加到这个元素,它需要被编译。 –

回答

1

$transclude功能必须提供给$compile。它传递给link函数。

link: function(scope, elem, attrs, $transclude) { 
    [...] 
    if ($transclude) { // It can be undefined if not content is provided 
    $compile(element, $transclude)(scope); 
    } 
    [...] 
} 

如果你想更深入的transclusion业务内容,按照这个link