Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将$watch设置为scope变量会导致watch代码立即执行,如何避免?_Javascript_Angularjs - Fatal编程技术网

Javascript 将$watch设置为scope变量会导致watch代码立即执行,如何避免?

Javascript 将$watch设置为scope变量会导致watch代码立即执行,如何避免?,javascript,angularjs,Javascript,Angularjs,我有这样一个angularjs代码: // some procedure called on page load... rectLayout(svg, scope.xdim, scope.ydim, scope.numOrder); // Watch changes of relevant variables: if any of basic variables changed, redraw the whole thing. scope.$watch(['xdim', 'dimY'

我有这样一个angularjs代码:

 // some procedure called on page load...

 rectLayout(svg, scope.xdim, scope.ydim, scope.numOrder);
 // Watch changes of relevant variables:  if any of basic variables changed, redraw the whole thing.
 scope.$watch(['xdim', 'dimY', 'numOrder'], function () {
      rectLayout(svg, scope.xdim, scope.ydim, scope.numOrder);
 }, true);

此代码导致在一行中调用rectLayout两次。我需要$watch稍后工作,但在页面初始化期间,我只希望第一次调用发生。我如何才能做到这一点?

您希望执行以下操作:

scope.$watch(['xdim','dimY', 'numOrder'], function(newVal, oldVal) {
  if (newVal !== oldVal) {
    rectLayout(svg, newVal.xdim, newVal.ydim, newVal.numOrder);
  }
});
顺便说一下,我不确定newVal是散列而不是数组。你应该仔细检查一下,但无论如何,这是一般的模式