0

I have an array and a watcher in my $scope. Watcher detects changes on the array property. Watcher works as expected: triggers listener when i update array contents.

When I assign my $scope to some other variable (var newScope = $scope), however, and then push an item to newScope's array , the watcher is not called.

Javascript should assign $scope to newScope by reference right? So, $scope and newScope will refer to the same object in memory. Then, why watcher is not called when I modify array on newScope?

rayners
  • 539
  • 2
  • 13
Suren Aznauryan
  • 206
  • 3
  • 10

1 Answers1

0

If you assign $scope to newScope then watching $scope won't do anything unless you update $scope. The reference is no longer $scope you want to be watching newScope

Shan Robertson
  • 2,742
  • 3
  • 25
  • 43
  • Javascript should assign $scope to newScope by reference right? So, $scope and newScope will refer to the same object in memory. Isn't that right? – Suren Aznauryan Dec 17 '14 at 08:35
  • They both exist in memory yes, but still as their own variables. So calling newScope doesn't refer to $scope even though it has all it's properties & methods. For example, if you were to use an angular method like location (i'm fairly certain location updates in scope), It won't modify `newScope` it will modify $scope because that is the original object that angular works with. – Shan Robertson Dec 17 '14 at 16:46