Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
977 views
in Technique[技术] by (71.8m points)

performance - how about using Angular2 + JQuery together?

I am using Angular2. I would like to implement group panel collapsable. Toward it, there are a lot of examples using JQuery. However, I would like to implement it using original javascript. To use JQuery, I have to import the JQuery library too which might cause some additional loading. I think there may be some DOM manipulation in my future development also besides group panel. I would like to know which is better using javascript or JQuery. I know there is some trade-off between them. In the respect of performance, which way would be recommended. Using Angular2 and JQuery together is recommended approach? Please give me some advice.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Angular expects to be in control of the DOM, and its philosophy of how the page and the data interact mixes poorly with jQuery (in jQuery you tend to make a lot of direct DOM modifications, while in Angular the DOM is almost a side-effect representation of the scope data; modifications to the DOM made outside Angular's expected path can get blown away on the next digest, or can overwrite or destroy bindings that Angular depends on.)

Manipulating the DOM separately through jQuery is possible, so long as you use great care not to interfere with the Angular lifecycle -- but it's highly error prone unless you have a very strong understanding of Angular's DOM lifecycle and of what is and isn't safe to change. It's rarely necessary; it's almost always better to use Angular's own tools.

(Also note that Angular includes jqLite; much of the time the jQuery function you're looking for is already available without needing to install the full library.)

(If you must use jQuery, keep it within specific Components' afterViewInit function and never try to e.g. modify the results of one component from within another.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...