Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

angular.copy element causes illegal invocation error. #8353

Closed
noducks opened this issue Jul 26, 2014 · 4 comments
Closed

angular.copy element causes illegal invocation error. #8353

noducks opened this issue Jul 26, 2014 · 4 comments

Comments

@noducks
Copy link

noducks commented Jul 26, 2014

I think this is probably a bug? Am using 1.3.0beta-16. This code:

var a = angular.element('<div>A</div>');
angular.copy(a);

causes this error:

TypeError: Illegal invocation
at forEach (http://localhost:8080/bower_components/angular/angular.js:330:13)

whereas if I use underscore.js, it seems to clone the same object without a problem.

_.clone(a);
@noducks noducks changed the title angular.copy an element causes illegal invocation error. angular.copy element causes illegal invocation error. Jul 26, 2014
@pkozlowski-opensource
Copy link
Member

@noducks this is kind of "interesting" corner case... The trouble is that you are trying to copy a jQuery / jqLite DOM element treating it like a regular JS object. May I ask what is your use-case here? If you need to clone a jQuery / jqLite element, you should probably use a clone method on en element: a.clone().

I'm not sure if so much more can be done on the AngularJS side as "Illegal invocation" sounds about right to me in this case.

Don't get me wrong: I'm not saying things can't be improved on the AngularJS side, but I would like to understand your use case better before proceeding.

@noducks
Copy link
Author

noducks commented Jul 26, 2014

@pkozlowski-opensource - I'm developing an 'educational' flashcard application. I have a queue of cards that get displayed. If the user gets one wrong I show it again after a period of time. I thought a handy way to do this would be to just clone the card (element and all) and throw it on the end of the queue. This had the advantage of keeping scoring data, undo data, etc in one big queue.

Am fairly happy that there are many other ways to implement this. Just happened to notice the error doing it that way, and thought I'd mention it in case it was a bug.

By the way, thanks for the quick response.

@caitp
Copy link
Contributor

caitp commented Jul 26, 2014

@noducks the primary use-case for those methods is internal stuff within angular --- it's better if you use lodash or jquery or Object.assign instead, typically

@pkozlowski-opensource
Copy link
Member

@noducks from how it sounds I would presume that you try to do lower-level DOM manipulation from your JS code which is no-no with AngularJS. What you should be rather doing is to copy object model and let AngularJS do the DOM-related stuff. If you stick to manipulating data model angular.copy will work just fine.

Anyway, I'm quite happy with the error you get from AngularJS as it tries to tell you here that something is not right. I'm going to close this issue but I would suggest putting your code in a minimal http://plnkr.co/ and asking at SO or on IRC if you still need help with your use-case.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants