AngularJS - why manipulating DOM in controller is a bad thing?

后端 未结 3 529
执笔经年
执笔经年 2020-12-18 22:29

Its common knowledge that DOM manipulations should not be performed in AngularJS Controller, its very hard however to find why exactly is it a bad thing. All the sources say

3条回答
  •  感动是毒
    2020-12-18 23:08

    If you look at the ever so popular question "Thinking in AngularJS" if I have a jQuery background? you will get some hints.

    One of the biggest factor that i think DOM manipulation is neither needs nor done is because Angular uses declarative approach when it comes to DOM linking as against the imperative approach that you would use with direct DOM manipulation. Some of the answers detail this difference between declarative and imperative approach.

    With jQuery you tell the DOM what needs to happen, step by step. With AngularJS you describe what results you want but not how to do it. More on this here. Also, check out Mark Rajcok's answer.

    A more comprehensive treatment of this topic is also available here What is the difference between declarative and imperative programming

    With such an approach the controller implementation is simplified, and we start to get real value as code base size grows and complexity increases.

提交回复
热议问题