简体   繁体   中英

How to create a Javascript Node in Angular.JS

Simple question, I essentially want to do something like the following, but in "angularness"

var p = document.createElement("p");
document.body.appendChild(p);

I have the appendChild part, I just don't know how to create an element that is of type Node and didn't find anything on it. I know you can create an element using angular.element("<div></div>") however the problem with that is that is not of type Node and the appendChild method will not accept it.

With Angular's jqlite you would do the similar thing as with jQuery.

var p = angular.element("<p>")[0]; // now it's node
document.body.appendChild(p);

And conversely

var p = document.createElement("p");
angular.element(document.body).append(angular.element(p));

If you've got p from the outside and don't know exactly if it is DOM or jqLite element, use angular.element wrapper for it anyway (again, as you would do with jQuery).

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