angular trigger changes with $watch vs ng-change, ng-checked, etc

后端 未结 4 896
遥遥无期
遥遥无期 2020-12-04 19:10

Currently we could monitor data changes with several ways. We could trigger model changes with $watch and we could add directives to elements and bind some acti

4条回答
  •  误落风尘
    2020-12-04 19:54

    Directives like ng-change are used for data-binding to DOM. $watch is used in your JS code to listen for changes.

    When you need to have the DOM be affected by a change in your scope, or need to have a change in DOM (eg. field selection) affect the scope, you would use a directive.

    If you need to trigger JavaScript actions from a change in scope, for example an ajax request, then you would use $watch in your controller (or service) to listen for the change.

提交回复
热议问题