Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular directive - asking for transclusion?

I get an error below and I don't understand why. Any ideas?

html,

<button ng-click="loadForm()">Load Directive Form</button>
<div data-my-form></div>

angular,

   app.directive('myForm', function() {
        return {
          replace:true,
          controller:function($scope){
            $scope.isLoaded = false;
            $scope.loadForm = function(){
              $scope.isLoaded = true;
            }
          },
          template: '<div ng-if="isLoaded" ng-include="\'form.php\'" ></div>',
          link:function(scope, element) {

          }
        }
    });

error,

Error: [$compile:multidir] Multiple directives [ngInclude, ngInclude] asking for transclusion on: <div data-my-form="" ng-if="isLoaded" ng-include="'form.php'">

a fix,

'<div><div ng-if="isLoaded" ng-include="\'form.php\'" ></div></div>'

but why do I have to wrap it in a div? is it a angular bug?

like image 762
Run Avatar asked Jan 10 '23 06:01

Run


1 Answers

The error is clear. There are two directives on the same element that is trying to use transclusion:

1. ng-if 
2. ng-include

An element can only apply one transclusion.

To fix, try this instead:

<div data-my-form="" ng-if="isLoaded">
   <div ng-include="'form.php'"> </div>
</div>
like image 75
pixelbits Avatar answered Jan 18 '23 07:01

pixelbits