angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

Invalid argument in IE when using $compile

Open Matsemann opened this issue 12 years ago • 32 comments

A fiddle with a trivial example: http://jsfiddle.net/1t4crp6e/

<div ng-app="myApp">
    <div ng-controller="myctrl">
        <div test>the value is: {{testmodel}}</div>

        <br>
        <input type="text" ng-model="testmodel">
    </div>
</div>
....
link: function(scope, elm, attrs) {
            var standardText = elm.text();

            elm.html('something');

            $timeout(function() {
                console.log(standardText);
                elm.html('').append($compile('<span>' + standardText + '</span>')(scope.$parent));
            }, 2000);
        }

Wait 2 secs, see the content of directive change to a binding. In Firefox, everything works. In IE it works, but it throws a lot of errors in the console. (Don't worry about what I'm doing here, I just made a small example showing the issue)

Error: Invalid argument. at Anonymous function (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js:62:80) at k.prototype.$digest (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js:109:350) at k.prototype.$apply (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js:112:343) at Anonymous function (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js:18:158) at d (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js:34:474) at c (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js:18:66) at fc (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js:18:270) at Xc (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js:17:362) at Anonymous function (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js:214:78) at o (https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js:2:14725)

However, most of my tries of actually writing this worked in all browsers except IE. For instance, if I remove the span in the compile, and instead of elm.text() use elm.contents(). http://jsfiddle.net/1t4crp6e/1/ Only changed line 13 and 19 in the js, breaks horribly in IE. It's almost impossible to compile stuff in IE.

Am I doing something wrong? And any workarounds?

Matsemann avatar Aug 18 '14 19:08 Matsemann

Would love to see an official fix for this as well. We are using this for a large-scale Angular app and the interim solutions will be a pain to use.

wwwillchen avatar Oct 21 '15 00:10 wwwillchen

+1 on that issue here, we also have a very large AngularJS app to maintain.

silentHoo avatar Nov 02 '15 10:11 silentHoo

For people experiencing this issue, I would recommend you simply use the work-around of ng-bind. There are actually a few reasons it's better than {{ }} anyway.

kentcdodds avatar Nov 02 '15 13:11 kentcdodds

Hey kentcdodds, ng-bind can be a valid solution but when you have to use $compile on a lot of code changing all {{ }} to ng-bind can be frustrating. The above solution fixed the issue for me. The difference in the jQuery implementation between .html() and .empty().append() seems to do the trick on IE for me.

bogdanmatra avatar Nov 03 '15 15:11 bogdanmatra

I've tested the fix that @siemiatj provided, but it makes a ton of other tests fail, so we cannot use it. Given that there are plenty of workarounds for this issue, this issue doesn't have high priority. I'll invite you to build on the PR and find a solution, though: https://github.com/angular/angular.js/pull/13243

Narretz avatar Nov 03 '15 23:11 Narretz

Please correct me if I am wrong...

To me the observed behavior (the 'Invalid argument' error) does not seem to be a bug in IE or in angular at all.

Lets examine the code in the opening post:

  1. There is a div where a directive is applied to: <div test>the value is: {{testmodel}}</div>
  2. The div contains an angular expression: {{testmodel}}
  3. The directive then removes all child nodes of the div: elm.html('something');

(You could also do elm.html("") or elm.empty())

So what happens here?

  1. Angular must watch the scope variable "testmodell" for changes and must update the DOM node that contains the {{testmodel}} expression whenever the scope variable has changed. So angular creates a watch for the {{testmodel}} expression, which means it must also keep a reference to the DOM node that contains the {{testmodel}} expression, to be able to update it when the scope variable has changed.
  2. When the directive removes the DOM node (elm.html('something')) that contains the {{testmodel}} expression, angular does not know about the fact that this DOM node does not need to be updated anymore and that the watch for the expression should be removed. It still holds a reference to the DOM node that contains the {{testmodel}} expression and the watch on the scope variable is still active. But this DOM node (the text node containing {{testmodel}}) is no longer part of the DOM tree of the webpage.
  3. When the scope variable has been changed angular tires to update the DOM node (that is now not a part of the DOM tree of the web page anymore), because the watch on the testmodel scope variable is still active. Of course updating a detached (abandoned) DOM node is not a good idea. However, this seems to work in browsers like Chrome or FF, but fails in IE. It fails in IE for a good reason: The DOM node is not part of the DOM tree anymore (in some sense like a memory leak).

So to me it seems to not be a bug in IE or in angular, but rather in the code presented in the opening post. One should not modify DOM nodes directly where angular watches exist on. Instead angular should modify the DOM (via ng-if, ng-repeat, etc...), so it knows when removed DOM nodes do not need to be updated anymore.

However it's not always possible to let angular manage the DOM. But when we modify the DOM directly, we must in some way notify angular about it. The simplest way is to destroy the scope of removed DOM nodes.

So the solution would be to create a child scope for the DOM element that contains the {{testmodel}} expression. When we remove that DOM node, we can call $destroy() on the child scope and angular knows it must not update the DOM node any longer. Problem solved.

Well... Not completely solved. There seems to be some other cases where a similar problem happens to: https://github.com/angular/angular.js/issues/5025

rent-a-developer avatar Nov 12 '15 23:11 rent-a-developer

@rent-a-developer Thanks for investigating, I think you are right that this is actually expected behavior, it's just that many browsers tolerate it, while IE does not. Thinking about it again, the example in the OP includes a bad practice: namely re-compiling already compiled and linked DOM nodes. The interpolated text node is already compiled and linked before it is removed from the DOM, and then it is recompiled, which not only makes IE complain, but also adds another watcher on the scope, which is bad for perf reasons. If you want to extract text nodes / content from the DOM and re-compile it, you should either do it in the compile fn or make sure no other directive is executed before you do your stuff in the link fn: http://jsfiddle.net/1t4crp6e/8/

@lgalfaso Do you agree that this is actually expected behavior?

Narretz avatar Nov 13 '15 15:11 Narretz

I agree with @Narretz - that if you are going to modify the contents of the node and potentially recompile then you should be doing this in the compile function and probably terminating the compilation too. On top of that, whenever you intend to be recompiling elements during the application run then you need to create new scopes to ensure that they can be destroyed correctly to prevent leaks.

petebacondarwin avatar Nov 18 '15 17:11 petebacondarwin

Based on the explanations of @rent-a-developer and @petebacondarwin and my recommendations, I will close this issue as "wontfix"

Narretz avatar Nov 26 '15 16:11 Narretz

I recently came across the same issue rendering a kendo treeview with an angular template.

The template used to be:

<span k-template>
    <span id="{{dataItem.id + '_' + dataItem.entityType}}"
          ng-attr-title="{{dataItem.title}}">
        {{dataItem.text}}
    </span>
</span>

Adding an ng-if in there fixed the issue:

<span k-template>
    <span ng-if="dataItem"
          id="{{dataItem.id + '_' + dataItem.entityType}}"
          ng-attr-title="{{dataItem.title}}">
        {{dataItem.text}}
    </span>
</span>

Cerbrus avatar Dec 01 '15 10:12 Cerbrus

+1

askucher avatar Feb 03 '16 22:02 askucher

I'm going to lock this issue. Please see the explanations in https://github.com/angular/angular.js/issues/8659#issuecomment-156267569 and the recommended course of action in https://github.com/angular/angular.js/issues/8659#issuecomment-157800230. If you see any 3rd party module doing this (re-compiling a node), then this is a problem that should be pointed out to them. If you think you've found a genuine bug, please open a new issue.

Narretz avatar Feb 04 '16 08:02 Narretz