Search code examples
angularjsangularjs-ng-transclude

Ng-transclude with nested directives


I am working with 2 directives where one directives template contains the second directive. I would like to use ng-transclude inside the second (nested) directive. How can I accomplish this? Here is a plnkr.

<body ng-app="transcludeExample">
<script>
  angular.module('transcludeExample', [])
   .directive('pane', function(){
       return {
       restrict: 'E',
       transclude: true,
       scope: { title:'@' },
       template: '<div style="border: 1px solid black;">' +
                '<div style="background-color: yellow">{{title}}</div>' +
                '<test></test>' +
                //'<ng-transclude></ng-transclude>' +
              '</div>'
       };
  })
  .directive('test', function(){
       return {
         restrict: 'E',
         transclude: true,
         template: '<div><ng-transclude></ng-transclude></div>'
       };
  })
  .controller('ExampleController', ['$scope', function($scope) {
      $scope.title = 'Lorem Ipsuma';
      $scope.text = 'Neque porro quisquam est qui dolorem ipsum quia dolor...';
  }]);
</script>
<div ng-controller="ExampleController">
  <input ng-model="title"> <br/>
  <textarea ng-model="text"></textarea> <br/><br/><br/><br/><br/><br/>

  <pane title="{{title}}">{{text}}</pane>
</div>
</body>

Solution

  • Don't know if I'm missing something, but this seems to work:

    template: '<div style="border: 1px solid black;">' +
                '<div style="background-color: yellow">{{title}}</div>' +
                '<test><ng-transclude></ng-transclude></test>' +
              '</div>'