AngularJS $ watch vs $ watchCollection: Was ist besser für die Leistung?

Für die Beobachtung eines Objekts Gültigkeitsbereich variable, ist $scope.$watch mit objectEquality auf true gesetzt ODER $scope.$watchCollection besser?

Für eine $scope object-variable (wie 15 Attribute, einige geschachtelte 2 Ebenen tief) aktualisiert, mit Eingabe-Elementen und - ng-model in der Ansicht, wie schlimm ist $scope.$watch mit objectEquality eingestellt true? Ist das eine große Sache zu vermeiden?

Ist $watchCollection eine bessere Lösung?

Ich bin auf der Suche nach einfachen Gewinne zur Verbesserung der Leistung auf meinem AngularJS-App (ich bin immer noch nicht auf v1.2.2).

  //ctrl scope var
  $scope.filters = {
    name: '',
    info: {test: '', foo: '', bar: ''},
    yep: ''
    //etc ...
  }

  //ctrl watch ?
  $scope.$watch('filters', function(newVal, oldVal) {
    if(newVal !== oldVal) {
      //call with updated filters
    }
  }, true);

  //or ctrl watch collection ?
  $scope.$watchCollection('filters', function(newVal, oldVal) {
    if(newVal !== oldVal) {
      //call with updated filters
    }
  });

  //view input with ng-model
  <input type="text" ng-model="filters.name" />
  <input type="text" ng-model="filters.info.test" />
  <input type="text" ng-model="filters.yep" />
  //etc ...  

InformationsquelleAutor der Frage wbeange | 2014-10-23

Schreibe einen Kommentar