另一个transclude指令

时间:2016-03-01 10:46:20

标签: javascript angularjs transclusion

我在另一个指令中有一个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没有。我希望文本“按钮”包含在my-button内,然后my-button可以扩展为按钮。相反,我看到一个空白指令:

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

我希望

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

我的问题是:

我对此有何误解?它是否与指令按角度扩展的顺序有关?我能改变一下吗?

如何在另一个被转换的指令中实现包含转换的指令。

1 个答案:

答案 0 :(得分:2)

我认为你可以解决这个问题:

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