I'm having trouble getting ng-transclude to work within an ng-switch-default directive. Here's my code:
Directive:
.directive('field', ['$pile', function($plile) {
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:
I'm having trouble getting ng-transclude to work within an ng-switch-default directive. Here's my code:
Directive:
.directive('field', ['$pile', function($plile) {
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
Share Improve this question edited Nov 15, 2013 at 11:03 jonnybro asked Nov 15, 2013 at 10:30 jonnybrojonnybro 3122 silver badges10 bronze badges 2- Could you post a live demo? It would be easier to test and find a solution. – elclanrs Commented Nov 15, 2013 at 10:40
- @elclanrs Here ya go... plnkr.co/edit/3CEj5OY8uXMag75Xnliq?p=preview – jonnybro Commented Nov 15, 2013 at 11:04
2 Answers
Reset to default 6The 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>
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