Ng-click vs watch in Angular

I have a directive that loads a JSON file and then uses this data to create an HTML5 canvas picture (i.e. json data contains things like text, color, position). I also have several input fields (text, sliders, etc.) that allow the user to manipulate the drawing. I see that I can either $ watch each of these elements, or use ng-click and call a function - is there a recommended approach?

Some of the possible notes:

  • The form element and canvas are part of the same directive template.
  • Form elements respond to changes, so the submit button
  • Each of these element element values ​​gets validated, can be converted, and then modifies the json string. Then I call the update function, which reloads my canvas with the new data.

Maybe I'm wrong too ...

+5
source share
1 answer

So, from what I understand, there are some user actions on some elements (inside the directive), and you have to do something every time these events are triggered.

The purpose of $ watch is to “watch” / do something every time a particular variable value changes. $scope.$watch('watchedVariable', onWatchedVariableChangedFn)where onWatchedVariableChangedFn is a function. This only works when the value really changes, regardless of what causes the change.

, , ng-click='onClickFn()'. . [ , ng-click . , (, ..).]

, , / , , .

: , , , , - ($ emit/$broadcast) ($ on) , .

+7

All Articles