Angular directive - asking for transclusion?

99封情书 提交于 2020-01-04 04:16:10

问题


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?


回答1:


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>


来源:https://stackoverflow.com/questions/27456294/angular-directive-asking-for-transclusion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!