Javascript 角度$scope.$watch on数组调用更新函数

Javascript 角度$scope.$watch on数组调用更新函数,javascript,arrays,angularjs,angularjs-scope,angularjs-watch,Javascript,Arrays,Angularjs,Angularjs Scope,Angularjs Watch,在中,我有一个整数数组,我将$scope.$watch附加到该数组。更新阵列时,我的$scope.$watch不会触发console.log。为什么不调用my console.log <!DOCTYPE html> <html> <head> <script data-require="angularjs@1.5.0" data-semver="1.5.0" src="https://cdnjs.cloudflare.com/ajax/li

在中,我有一个整数数组,我将$scope.$watch附加到该数组。更新阵列时,我的$scope.$watch不会触发console.log。为什么不调用my console.log

<!DOCTYPE html>
<html>

  <head>
    <script data-require="angularjs@1.5.0" data-semver="1.5.0" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.2/angular.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body ng-app="test" ng-controller="testArray">

    <div ng-repeat="item in MyArray track by $index"> {{item}}</div>
    <button ng-click="add()">testing Add</button>
  </body>

</html>
<script type="text/javascript">
    'use strict';
    var app = angular.module('test', []);

    app.controller('testArray',['$scope',function($scope){

      $scope.MyArray = [1,2,3,4]

      $scope.add = function(){
        $scope.MyArray.push($scope.MyArray.length+1);
      }
    $scope.$watch('MyArray' , function(){
      console.log($scope.MyArray);
    })
    }]);

</script>

{{item}}
测试添加
"严格使用",;
var app=角度模块('测试',[]);
app.controller('testArray',['$scope',函数($scope){
$scope.MyArray=[1,2,3,4]
$scope.add=函数(){
$scope.MyArray.push($scope.MyArray.length+1);
}
$scope.$watch('MyArray',function()){
log($scope.MyArray);
})
}]);
将代码更改为

$scope.$watch('MyArray' , function(){
    console.log($scope.MyArray);
}, true)
查看,您将看到第三个参数指示$watch方法将使用对象相等性来确定数组是否已更改。这意味着angular将使用支持数组比较的
angular.equals
方法,例如将代码更改为

$scope.$watch('MyArray' , function(){
    console.log($scope.MyArray);
}, true)

查看,您将看到第三个参数指示$watch方法将使用对象相等性来确定数组是否已更改。这意味着angular将使用支持数组比较的
angular.equals
方法,例如您可以使用
$watchCollection
,只需在
$watch
函数中提供
true
选项,它将比deep watcher更便宜

$scope.$watchCollection('MyArray' , function(){
    console.log($scope.MyArray);
})
$watchCollection()
深入一层,对集合中的顶级项执行额外的浅层引用检查


如果您有一个非常大的阵列,您想在其中保持监视,那么不要使用
true
(深度监视程序)选择
$watch
。对于
1000
/
2000
记录,您会感到角度绑定滞后。因此,首选的方法是尽量避免使用
watcher
,或者只需使用
$watchCollection
您可以使用
$watchCollection
,只需在
$watch
函数中提供
true
选项,就可以比深度观察程序便宜

$scope.$watchCollection('MyArray' , function(){
    console.log($scope.MyArray);
})
$watchCollection()
深入一层,对集合中的顶级项执行额外的浅层引用检查


如果您有一个非常大的阵列,您想在其中保持监视,那么不要使用
true
(深度监视程序)选择
$watch
。对于
1000
/
2000
记录,您会感到角度绑定滞后。因此,首选的方法是尽可能避免
观察者
,或者只要选择
$watchCollection

,如果您将第三个参数设置为trus,这意味着angular将对对象进行深入观察,我决定接受@pankajParkers答案,因为watchCollection的内存性能超过了使用时间有棱角的copy@RenanLopesFerreira&gh9谢谢大家。如果您介绍了
deepwatcher
中与性能相关的内容,我就不会添加答案。所以不要编辑你的答案。。我添加了新的答案。:-)@PankajParkar我不知道$watchCollection,所以,谢谢!如果将第三个参数设置为trus,则意味着angular将对objectI进行深入监视。由于使用angular时watchCollection的内存性能,我决定接受@pankajParkers应答。copy@RenanLopesFerreira&gh9谢谢大家。如果您介绍了
deepwatcher
中与性能相关的内容,我就不会添加答案。所以不要编辑你的答案。。我添加了新的答案。:-)@PankajParkar我不知道$watchCollection,所以,谢谢!