Why is two-way databinding in AngularJS an antipattern?

前端 未结 2 533
花落未央
花落未央 2021-02-02 10:46

AngularJS offers two-way databinding.

I built several AngularJS apps and found two-way databinding to be a powerful feature, that increased my productivity.

Rece

2条回答
  •  借酒劲吻你
    2021-02-02 11:12

    In fact, the main problem with two-way data binding is performance.

    When AngularJS was released (1), this feature was the foremost reason why the framework was much used by developers.

    Without a line of code, you can make an element fully dynamic by changing its value from the model side or the view side, the value is changed everywhere the model is set.

    In this feature, the most important tool is the watching, and it represents all the problem with two-way data binding.

    As the application evolves, the number of watchers and watched elements increases.
    Also, after a time, the application can become a big soup of watchers.
    This will result in your application always watching elements and keeping up-to-date the elements at the inverse side, and that consumes a lot of resources from the browser.

    This is why my recommendation is: Avoid watchers as much as possible.
    They are almost never really necessary in a controller.

    See also :

    • Effective strategies to avoid watches in AngularJS
    • The bad parts of AngularJS
    • Performances in large AngularJS applications

    Hope it's more clear for you.

提交回复
热议问题