嵌套指令抛出错误:[$ compile:multidir]

时间:2015-03-15 15:12:59

标签: javascript angularjs angularjs-directive

我有一个非常简单的嵌套指令,它只是抛出一个错误。我可以毫无错误地调用base-directive,但是当我添加调用base-directive的my-directive时,我得到了错误。

<div ng-controller="MainController">
    <base-directive>This is the base directive</base-directive> 
    <my-directive>this is my directive</my-directive> 
</div>

http://plnkr.co/edit/ZDKNj0Ki4mRgudmTTUAP

angular.module('MyApp', [])
    .directive('baseDirective', function() {
        return {
            restrict: 'E',
            replace: true,
            transclude: true,
            template   : '<div>base-directive:transclusion=<ng-transclude></ng-transclude></div>'
        };
    })
    .directive('myDirective', function() {
        return {
            restrict: 'E',
            replace: true,       
            transclude:true,
            template   : '<base-directive><ng-transclude></ng-transclude></base-directive>'
        }
    })
    .controller('MainController', ['$scope', function($scope) {
      }])

这是错误:

错误:[$ compile:multidir]多个指令[myDirective,baseDirective]要求进行翻译: http://errors.angularjs.org/1.4.0-beta.5/ $编译/ multidir P0 = myDirective&安培; P1 = baseDirective&安培; P2 = transclusion&安培; P3 =%3Cbase-指令%3E     在REGEX_STRING_REGEXP(angular.js:68)     在assertNoDuplicate(angular.js:8065)     at applyDirectivesToNode(angular.js:7480)     在compileNodes(angular.js:7117)     在compileNodes(angular.js:7129)     在compileNodes(angular.js:7129)     在编译时(angular.js:7024)     在angular.js:1519     在Scope。$ get.Scope。$ eval(angular.js:15236)     在Scope。$ get.Scope。$ apply(angular.js:15335)

我正在尝试做什么:

我希望创建一些基本指令来呈现UI的一部分,以及一些更高级别的指令,它们调用这些基本指令来构建完整的“小部件”。

更新1 - 没有多次转换

正如@Claies所建议的那样,我不能进行多次转换,但它必须不止于此,因为即使我在文本中传递此版本,并且只在基础中进行了转换,我也会收到错误:

.directive('myDirective', function() {
    return {
        restrict: 'E',
        replace: true,       
        scope: {
          text: '@' 
        },
        template   : '<base-directive>{{text}}</base-directive>'
    }
})
.controller('MainController', ['$scope', function($scope) {
  }])

<div ng-controller="MainController">
    <base-directive>This is the base directive</base-directive> 
    <my-directive text="this is my directive"></my-directive> 
</div>

Error: [$compile:multidir] Multiple directives [myDirective, baseDirective] asking for template on: <base-directive text="this is my directive">

0 个答案:

没有答案
相关问题