Angular transclude 指令在另一个 transclude 指令中

Angular transclude directive inside another transclude directive

我在另一个指令中有一个 angular 指令。两者都使用 transclude: true.

我希望如果我有以下代码(取自 plunker)我会看到同样的东西 3 次。

https://plnkr.co/edit/iIyU65WdMr4jDQyKZpt1?p=preview

JS:

angular.module('app', [])

.directive('myButton', myButton)

.directive('directiveWithDirective', directiveWithDirective)

.directive('directiveWithDiv', directiveWithDiv);

function myButton(){
  return {
            restrict: 'E',
            transclude: true,
            template: '<button ng-transclude> </button>'
        };
}

function directiveWithDirective(){
  return {
            restrict: 'E',
            transclude: true,
            template: '<my-button ng-transclude> </my-button>'
        };
}

function directiveWithDiv(){
  return {
            restrict: 'E',
            transclude: true,
            template: '<div ng-transclude> </div>'
        };
}

HTML:

<div ng-app="app">
  <my-button>
    A Button
  </my-button>
  <br>
  <directive-with-directive>
    A Button
  </directive-with-directive>
  <br>
  <directive-with-div>
    <div>
      <my-button>
        A Button
      </my-button>
    </div>
  </directive-with->
</div>

my-buttondirective-with-div 的行为符合我的预期。他们将自己的内容包含在模板中。

然而,directive-with-directive 没有。我希望文本 "a button" 包含在 my-button 中,然后 my-button 扩展为一个按钮。相反,我看到一个空白指令:

<my-button ng-transclude=""> </my-button>.

我期待

<my-button ng-transclude=""><button>A Button</button> </my-button>

我的问题是:

我对此有什么误解?它与angular扩展指令的顺序有关吗?我可以更改吗?

我怎样才能在另一个被嵌入的指令中包含一个指令。

我想你可以用这个解决你的问题:

function directiveWithDirective(){
  return {
            restrict: 'E',
            transclude: true,
            template: '<my-button><ng-transclude /> </my-button>'
        };
}