$watch not being triggered on array change

后端 未结 4 1789
北荒
北荒 2020-12-08 00:29

I\'m trying to figure out why my $watch isn\'t being triggered. This is a snippet from the relevant controller:

$scope.$watch(\'tasks\', functi         


        
4条回答
  •  情歌与酒
    2020-12-08 01:14

    Very good answer by @Mark. In addition to his answer, there is one important functionality of $watch function you should be aware of.

    With the $watch function declaration as follows:

    $watch(watch_expression, listener, objectEquality)
    

    The $watch listener function is called only when the value from the current watch expression (in your case it is 'tasks') and the previous call to watch expression are not equal. Angular saves the value of the object for later comparison. Because of that, watching complex options will have disadvantageous memory and performance implications. Basically the simpler watch expression value the better.

提交回复
热议问题