简体   繁体   中英

How to add/ remove createElement from DOM in Angular.js

In my angular directive, I'm creating an external script from DOM for a particular object in a list, in which it adds 'script' tag and div content. I'm able to add it but I can't remove the scripts. I'm using a button with a list of object, let say button contains list of {a,b,c}. I want to add DOM element for a, remove DOM element for b and add it for c when clicked. My question is how can I create & remove DOM element for appending scripts.

My code for creating element is

var a = document.createElement('script');
a.src = 'errors.js' // here 22 &223 are codes extracted from errors.js         
a.setAttribute('source', 22);
a.setAttribute('source', 223);
var e = document.getElementsByTagName('script')[0];
e.parentNode.insertBefore(a,e);

This is what I'm using for creating an element. Is there a better way to do in Angular.js? I'm trying to remove the created element and its attributes.

Thanks!

You can remove angular.element("script").remove() to remove an element based on selector

Same way angular.element("selector").append( "Your script tag" ) to add elemement

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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