javascript - Setting $watch to scope variables causes watch code to execute immediately, how to avoid? -
i have angularjs code this:
// procedure called on page load... rectlayout(svg, scope.xdim, scope.ydim, scope.numorder); // watch changes of relevant variables: if of basic variables changed, redraw whole thing. scope.$watch(['xdim', 'dimy', 'numorder'], function () { rectlayout(svg, scope.xdim, scope.ydim, scope.numorder); }, true);
this code causes rectlayout called twice in row. need $watch working later, during initialization of page want first call happen. how can achieve that?
you want following:
scope.$watch(['xdim','dimy', 'numorder'], function(newval, oldval) { if (newval !== oldval) { rectlayout(svg, newval.xdim, newval.ydim, newval.numorder); } });
i'm not sure, way, newval hash , not array. should double check that, in case, that's general pattern.
Comments
Post a Comment