5

I have an element which its whole content could be transcluded. the transclusion is optional, so I can let its inner parts stay but an inner element be transcluded. Let's see it in action:

    <h4 class="modal-title" ng-transclude="title">
    Are you sure you want to remove this <span ng-transclude="innerType">good</span>
    </h4>

In directive definition I have:

    myApp.directive('confirmDeleteModal',function(){
    return {
        restrict:'E',
        transclude: {
           title:'?modalTitle',
           innerType:'?modalType',
        },
        templateUrl:'templates/confirm_delete_modal.html',
        scope: {
           obj:'=info',
        },
     }
   });

The HTML code would be so:

  <confirm-delete-modal info="goodToBeDeleted">
     <modal-type>good</modal-type>
  </confirm-delete-modal>

When I run my code, I get the following error: [ngTransclude:orphan].

What should I do? I am using AngularJS v1.5.8

Michiel
  • 4,160
  • 3
  • 30
  • 42
rastemoh
  • 438
  • 3
  • 10
  • Is tranclude set to true? See https://docs.angularjs.org/error/ngTransclude/orphan – Zdenek Hatak Aug 11 '16 at 07:41
  • @ZdenekHatak I have passed an object containing my slots so there is no need to set transclude to true, see Multi-slot transclusion in [API reference]https://docs.angularjs.org/api/ng/directive/ngTransclude – rastemoh Aug 11 '16 at 07:43
  • Can you provide HTML dom that call your directive ? And the complete configuration of your directive? And last thing, what version of angular do you use ? Because multi sloting is available since 1.5 – Silvinus Aug 11 '16 at 07:47
  • @Silvinus I made the changes – rastemoh Aug 11 '16 at 07:57

1 Answers1

1

You use another directive ng-transclude in a fall back content of modal-title. But the ng-transclude become the "parent" of the span and doesn't provide transclusion function. I suggest to you to modify your directive and to use the method isSlotFilled to know if title is filled or not :

directive('confirmDeleteModal',function(){
return {
  restrict:'E',
  transclude: {
    title:'?modalTitle',
    innerType:'?modalType',
  },
  link: function(scope, elem, attrs, ctrl, trfn) {
    scope.titleFilled = trfn.isSlotFilled('title');
  },
  template:'<h4 class="modal-title" ng-transclude="title" ng-if="titleFilled"></h4>' +
           '<h4 class="modal-title" ng-if="!titleFilled">Are you sure you want to remove this <span ng-transclude="innerType">good</span></h4>',
  scope:{
      obj:'=info',
  }
 }
})

(https://plnkr.co/edit/k0RXLWbOvHdNc9WFpslz?p=preview)

Silvinus
  • 1,445
  • 8
  • 16