简体   繁体   English

在指令中包装jQuery插件时出现AngularJS错误

[英]AngularJS error when wrapping jQuery plugin in a directive

I'm working on a directive for AngularJS that builds a taggable element and utilizes TagsInput 我正在为AngularJS编写指令,该指令构建可标记的元素并利用TagsInput

Here's a working fiddle: http://jsfiddle.net/mgLss/ 这是一个有效的小提琴: http : //jsfiddle.net/mgLss/

Not sure why but when I add that directive to my application IT works fine, but anything else below it running angular fails and I get this error message: 不知道为什么,但是当我将该指令添加到我的应用程序中时,IT可以正常工作,但是在它以下运行Angular的其他所有程序都会失败,并且出现以下错误消息:

Error: node is undefined
compositeLinkFn@http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js:3837
compositeLinkFn@http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js:3837
nodeLinkFn@http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js:4216
compositeLinkFn@http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js:3834
compositeLinkFn@http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js:3837
compositeLinkFn@http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js:3837
compositeLinkFn@http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js:3837
nodeLinkFn@http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js:4216
compositeLinkFn@http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js:3834
compositeLinkFn@http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js:3837
compile/<@http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js:3746
bootstrap/</<@http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js:932
Scope.$eval@http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js:7808
Scope.$apply@http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js:7888
bootstrap/<@http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js:930
invoke@http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js:2788
bootstrap@http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js:929
angularInit@http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js:904
@http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js:14397
f.Callbacks/o@http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js:2
f.Callbacks/p.fireWith@http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js:2
.ready@http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js:2
B@http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js:2

I've spent the last hour on IRC but cant get any acknowledgment of my question so here's hoping Stack will come to the rescue as it has so many times before. 我已经在IRC上度过了最后一个小时,但无法得到我的问题的任何认可,因此,希望Stack能够像以前一样多次进行救援。

This is something related to the plugin you are using, it manipulates the dom in a way angular does not like it, I didn't to go into the source code the point you to the root of the issue, to be honest. 这是与您使用的插件有关的东西,它以某种角度不喜欢它的方式操纵dom,老实说,我并没有进入源代码来指出问题的根源。 But here is a way (an ugly one) to fix it. 但是,这是一种修复方法(很丑)。

<div ng:controller="TestCtrl">
    {{ hello }}
    <div><taggable default="changed"></taggable></div>
</div>
​

Just wrap that directive within another DOM element, making sure the plugin is isolated. 只需将该指令包装在另一个DOM元素中,确保插件被隔离。

http://jsfiddle.net/mgLss/33/ http://jsfiddle.net/mgLss/33/

Building on @fastreload's answer, a slightly less ugly solution, which does not require changing your HTML: 以@fastreload的答案为基础,这是一个不太丑陋的解决方案,不需要更改HTML:

// as per @fastreload, wrap input in a div to prevent Angular from getting confused
template: "<div><input type=\"text\"></div>",
link: function(scope, elm, attrs) {
    elm.children().tagsInput({      // note children()

You should also include Angular last in your fiddle (under Manage Resources) (and in your code in general), then elm is automatically a wrapped jQuery element, rather than a wrapped Angular element, and hence we don't need to use $(elm) in the link function. 您还应该在您的小提琴中(在“管理资源”下)(以及通常在您的代码中)包括Angular最后,然后elm自动是一个包装的jQuery元素,而不是一个包装的Angular元素,因此我们不需要使用$(elm)在链接功能。

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

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