0

In the answer to Databinding in angularjs, @misko-hevery mentioned

'Suppose you have two arrays which need to be kept in sync for whatever reason, ...'.

I have a simliar problem. Say I have two arrays: knives and forks. I want to change them and fire only one event and update the UI only once. The sample code is here(http://jsbin.com/efolag/3/edit).

I copied the javascript code here:

var app = angular.module('app',[]);

function Main($scope){
  console.log("initialize controller Main");
  $scope.value="a";
  $scope.$watch("newItem",function(newValue){
    $scope.knives=["a","b","c", newValue];
    $scope.forks=["d","e","f", newValue];
  });
}

My question is how many dirty checking loops will the changes to knives and forks trigger? If it is more than once, is it possible to make it fire only once?

Community
  • 1
  • 1
Devs love ZenUML
  • 11,344
  • 8
  • 53
  • 67
  • tutorial on angular docs site will likely help. If you haven't been through it end to end is well worth the time spent Very difficult to answer your broad question without more context of how your UI is intended to work and how the 2 arrays interact. A simple demo also helps – charlietfl Apr 01 '13 at 15:11
  • Thank you for your comments. I added sample code here. – Devs love ZenUML Apr 01 '13 at 15:45
  • The change event(watches will only during the digest phase, that means in sample it will fire only once. You may test it by adding watches for both `knives` and `forks` – Arun P Johny Apr 01 '13 at 15:49

0 Answers0