Is it a bad practice to use jQuery in Angular? [cl

2020-02-07 00:22发布

My question is the following. Should I avoid using any kind of jQuery code in Angular application as it seems legit to have only one thing interacting with DOM. Another question is if anyone came across problems where he couldn't find any other solution but writing a quick hack with jQuery.

Thank YOU!

3条回答
劳资没心,怎么记你
2楼-- · 2020-02-07 00:25

The first thing you should do is to read this thread on SO "Thinking in AngularJS" if I have a jQuery background?. This will give you some perspective.

When it comes to Angular, it the model that drives the view and most of the times direct DOM manipulation is not required.

For example if you are using DOM manipulation to show\hide element, add remove class or set style, then better to use ng-show\ng-class\ng-style directive.

But there are cases when DOM manipulation is required and that is the time you write directives and either use jqLite or jQuery to manipulate DOM.

My suggestion would be to avoid jQuery unless you have to incorporate a jquery plugin that is dependent on jQuery.

While developing always look if the inbuilt directives that can serve your purpose. If not can jqLite be used to achieve what is desired. Your final resort should be jQuery.

查看更多
Deceive 欺骗
3楼-- · 2020-02-07 00:26

Well it's just two large resources, which makes your app "heavy". Otherwise it's only a preference thing. Personally I don't use jQuery with any of the reactive frameworks (Vue, React nor Angular).
Remember that anything jQuery can do, you can do with vanilla JS.

查看更多
家丑人穷心不美
4楼-- · 2020-02-07 00:40

Yes it's a bad practice, but sometimes it will save you much time, especially when you are looking for a plugin, Do it when necessary only, and keep a note to switch it back when other solutions are available.

查看更多
登录 后发表回答