5

I'm having trouble getting ng-transclude to work within an ng-switch-default directive. Here's my code:

Directive:

.directive('field', ['$compile', function($complile) {
        return {
            restrict: 'E',
            scope: {
                ngModel: '=',
                type: '@',
            },
            transclude: true,
            templateUrl: 'partials/formField.html',
            replace: true
        };
    }])

partials/formField.html

<div ng-switch on="type">
    <input ng-switch-when="text" ng-model="$parent.ngModel" type="text">
    <div ng-switch-default>
        <div ng-transclude></div>
    </div>
</div>

I call it like so...

<field type="other" label="My field">
    test...
 </field>

Which produces the error:

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

It works without a hitch, outside of the ng-switch directive, I'm at a loss on how to get this working though. Any suggestions?

EDIT: Here's a live demo: http://plnkr.co/edit/3CEj5OY8uXMag75Xnliq?p=preview

jonnybro
  • 312
  • 2
  • 10

2 Answers2

6

The problem is that ng-switch is doing its own transclusion. Because of this, your transclusion is getting lost with the transclusion of ng-switch.

I don't think you will be able to use ng-switch here.

You can use ng-if or ng-show instead:

<input ng-if="type == 'text'" ng-model="$parent.ngModel" type="{{type}}" class="form-control" id="{{id}}" placeholder="{{placeholder}}" ng-required="required">
<div ng-if="type != 'text'">
    <div ng-transclude></div>
</div>
Brian Genisio
  • 47,787
  • 16
  • 124
  • 167
  • 1
    It would appear this is a bug in the current version of the framework. I've logged an issue on Github here: https://github.com/angular/angular.js/issues/4969 ... Thanks for the workaround, I suppose it will have to do for now! – jonnybro Nov 16 '13 at 17:15
0

Taken from: Github issue

The problem is that ng-switch is using a transclude as well, which leads to the error.

In this case, you should create a new directive that uses the right $transclude function. For this to work, store the $transclude in the controller of your parent directive (in your case field), and create a new directive that references that controller and uses its $transclude function.

In your example:

.directive('field', function() {
  return {
       ....
      controller: ['$transclude', function($transclude) {
        this.$transclude = $transclude;
      }],
      transclude: true,
       ....
  };
})
.directive('fieldTransclude', function() {
  return {
    require: '^field',
    link: function($scope, $element, $attrs, fieldCtrl) {
      fieldCtrl.$transclude(function(clone) {
        $element.empty();
        $element.append(clone);
      });
    }
  }
})

In the html, you then just use <div field-transclude> instead of <div ng-transclude>.

Here is an updated plunker: http://plnkr.co/edit/au6pxVpGZz3vWTUcTCFT?p=preview

Lan
  • 1,206
  • 1
  • 11
  • 26
jonnybro
  • 312
  • 2
  • 10