【发布时间】:2016-05-09 11:38:06
【问题描述】:
所以我对 watchCollection 语句有点困惑。我正在观察我的数组对象的变化,并且我注意到每当我在 $watch 函数中使用 if 语句时,我都会删除数组的 1 个元素。我很困惑为什么 $watchCollection 函数是一个迭代过程,其中 in 重新运行数组的长度。我在下面有这个简单的手表功能:
scope.$watchCollection('waypoints', function () {
if (scope.waypoints.length > 1) {
if (scope.waypoints[scope.waypoints.length - 1] == "`") {
scope.waypoints.pop();
}
calculateAndDisplayRoute(directionsService, directionsDisplay, scope.waypoints);
}
});
当我运行 if 语句时,我从数组中丢失了一个元素。我什至创建了一个带有调试器的 jsfiddle,显示 $watch 函数会重复数组 http://jsfiddle.net/kL6909to/ 的长度。
【问题讨论】:
标签: angularjs scope angularjs-scope watch