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

前端 未结 5 1679
灰色年华
灰色年华 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条回答
  •  被撕碎了的回忆
    2021-02-19 21:26

    Underscore (or lo-dash) has a once function. If you wrap your function inside once you can ensure your function will be called only once.

    angular.module('app', []).
    
    directive('person', function() {
      return {
        restrict: 'A',
        link: function($scope, $elem, $attrs) {
            var action = function() {
              $elem.append('name: ' + $attrs.name + '
    surname: ' + $attrs.surname+'

    '); } var once = _.once(action); $attrs.$observe('name', once); $attrs.$observe('surname', once); } } });

提交回复
热议问题