$observe multiple attributes at the same time and fire callback only once

前端 未结 5 1720
灰色年华
灰色年华 2021-02-19 20:38

I wonder is it possible to execute some callback only once after evaluation all (or only some) attributes of directive (without isolated scope). Attributes are really great to p

5条回答
  •  -上瘾入骨i
    2021-02-19 21:18

    You can use $watch to evaluate a custom function rather than a specific model.

    i.e.

    $scope.$watch(function () {
      return [$attrs.name, $attrs.surname];
    }, action, true);
    

    That will be run on all $digest cycles, and if $watch detects the return array (or however you want to structure your function's return value) doesn't match the old value, the callback argument to $watch will fire. If you do use an object as the return value though, make sure to leave the true value in for the last argument to $watch so that $watch will do a deep compare.

提交回复
热议问题