简体   繁体   English

要求模板的多个指令

[英]Multiple directives asking for templates on

I have the following HTML: 我有以下HTML:

<div style="border:1px solid; height:300px; width:500px; position:relative;  left:100px" id="canvas">
  <tbox ng-repeat="tb in textBoxes" ng-model="tb">
  </tbox>
</div>

And the following 2 directives 以及2个指令

appModule.directive('resizable', function($compile, $document) {
  return {
    restrict: "A",
    template: '<div ng-style="{top:tb.x, left:tb.y, height:tb.height, width:tb.width}"  ng-transclude><span class="scale">s</span></div>',
    transclude: true,
    replace: true,
    require: 'ngModel'
  }
});

appModule.directive('tbox', function($document) {
  return {
    restrict: "E",
    template: '<div class="editbox" resizable editoptions>{{tb.text}}</div>',
    replace: true
  }
});

What exactly does the following error that angular is throwing me means? 究竟是什么角度投掷我的意思是什么?

Error: Multiple directives [tbox, resizable] asking for template on: <div class="editbox" resizable="" editoptions="" ng-repeat="tb in textBoxes" ng-model="tb">

jsfiddle at http://jsfiddle.net/sEZM3/ jsfiddle at http://jsfiddle.net/sEZM3/

Both of your directives are trying to replace the element in the dom. 你的两个指令都试图替换dom中的元素。 Try removing the the replace: true lines in your directive definition object. 尝试删除指令定义对象中的replace: true行。

The same error may occur if you try to load the same directive code more than once by mistake. 如果您尝试错误地多次加载相同的指令代码,则可能会出现相同的错误。 It may happen especially when you keep every Angular item (like directive) in separate file and you include every single one with separate line. 特别是当你将每个Angular项目(如指令)保存在单独的文件中并且每个单独的行包含单独的行时,可能会发生这种情况。 That was my case. 那是我的情况。

For me, this was caused by multiple copies of the directive and template existing in the dist directory caused by grunt building without cleaning (during a watch task). 对我来说,这是由dist目录中存在的指令和模板的多个副本引起的,这些副本是由没有清理的grunt构建引起的(在监视任务期间)。 A clean and rebuild solved this for me. 干净和重建为我解决了这个问题。

对我来说,它在index.html中包含了两次相同的指令。

Happened to me when I was having two components with the same name (copy paste error): 当我有两个具有相同名称的组件时(复制粘贴错误)发生在我身上:

For my coffeescript, but easy to happen in pure angular: 对于我的coffeescript,但很容易发生纯角度:

component1.coffee
    appModule.component('name', {
    templateUrl: '/public/partials/html1.html',
  controller: 'controler1',
  bindings: {
    somebindings: '<'
  }
});


component2.coffee
    appModule.component('name', {
    templateUrl: '/public/partials/html2.html',
  controller: 'controler2',
  bindings: {
    somebindings: '<'
  }
});

Conclusion: "name" has to be unique in whole app. 结论:“名称”在整个应用程序中必须是唯一的。

For Visual Studio users using TypeScript, this got me. 对于使用TypeScript的Visual Studio用户,这让我感到高兴。 I'd renamed my typescript file and the built .js file was in the directory (but it doesn't show up in the project). 我重命名了我的typescript文件,并且内置的.js文件位于目录中(但它没有显示在项目中)。 I had to show all files in the directory to remove the lingering unused *.js files. 我必须显示目录中的所有文件以删除遗留的未使用的* .js文件。

The resizable directive is incorrect. 可调整大小的指令不正确。 The ng-transclude directive must be applied to the innermost element, because its content will be discarded and replaced with transcluded content. ng-transclude指令必须应用于最内层元素,因为其内容将被丢弃并替换为已转换的内容。

You should surround tbox directive template with (corrected) resizable element directive. 您应该使用(更正的) 可调整大小元素指令来包围tbox指令模板。 I dont' know what editoptions attribute does, but if it's also a directive, then it also shouldn't have a template. 我不知道editoptions属性做了什么,但如果它也是一个指令,那么它也不应该有一个模板。

I mean something like this: 我的意思是这样的:

appModule.directive('resizable', function($compile, $document) {
    return {
        restrict: "E",
        template: '<div ng-style="{top:tb.x, left:tb.y, height:tb.height, width:tb.width}"  ng-transclude></div>',
        transclude: true,
        replace: true,
        //...

appModule.directive('tbox', function($document) {
    return {
        restrict: "E",
        template: '<resizable><div class="editbox" editoptions>{{tb.text}}</div></resizable>',
        replace: true,
        //...

Result: 结果:

<div ng-style="{top:tb.x, left:tb.y, height:tb.height, width:tb.width}"  ng-transclude>
    <div class="editbox" editoptions>{{tb.text}}</div>
</div>

在我的情况下,我在BundleConfig中引用了一个丢失的文件,我删除了引用并开始工作。

For me there were no duplicates in my code. 对我来说,我的代码中没有重复项。 This happend as a result of me duplicating a module to get a headstart on the new one, and then doing a module wide find/replace and changing the names of the files. 这是因为我复制了一个模块以获得新模块的headstart,然后在模块范围内查找/替换并更改文件的名称。

Even though there was no duplicate anymore, and I stopped and started the browsersync based server, the error continued. 即使没有重复,我停止并启动基于browsersync的服务器,错误仍在继续。

Resolving it was done by removing the .tmp directory that the build system was creating for the dev environment. 解决它是通过删除构建系统为开发环境创建的.tmp目录来完成的。

Evidently the FountainJS generator I'm using creates a build system that leaves the .tmp directory dirty in some cases like this. 显然我正在使用的FountainJS生成器创建了一个构建系统,在某些情况下会使.tmp目录变脏。 It's caught me a few times now. 它现在抓住了我几次。

它也可能是一个简单的错误,例如将templatetemplateUrl属性保存在同一个指令中。

(in case it helps someone else) (以防它帮助其他人)

For me the issue was having a backup file (ie .bkp.html) which was just an old copy of a template I was using for reference - but this was being included by karma since it matched the ".../**/*.html" pattern. 对我来说,问题是有一个备份文件(即.bkp.html),它只是我用来参考的模板的旧副本 - 但这是由karma包含的,因为它匹配“... / ** / * .html“模式。

You can't have one (element) directive directly referencing another (element) directive. 您不能让一个(元素)指令直接引用另一个(元素)指令。 Wrap it with a <div> , eg: <div>包裹它,例如:

template: '<div><my-custom-directive>'
        + '</my-custom-directive></div>'

See https://github.com/angular/angular.js/issues/5428 for more information. 有关更多信息,请参阅https://github.com/angular/angular.js/issues/5428

多次导入同一指令可能会导致此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM