1

According to this: How to deep watch an array in angularjs? it should work but i dont know what i am doing wrong...

    $scope.$watch('statuses', function(newVal, oldValue) {
      if (oldValue==newVal) return false;

      console.log("st changed");
    },true);

And let's say i somehow asynchronly update the $scope.statuses for example after 3seconds

setTimeout(function() {
    $scope.statuses.push("ahhh");
},3000);

And nothing happens...any ideas why please?

3 Answers 3

2

Use the following code:

setTimeout(function() {
  $scope.$apply(function () {
    $scope.statuses.push("ahhh");
  }
},3000);

You have to use $apply since AngularJS is unaware of update to $scope

As a convenience, AngularJS provides $timeout, which is like setTimeout, but automatically wraps your code in $apply by default.

Sign up to request clarification or add additional context in comments.

Comments

1
 $scope.statuses = [];

                $scope.$watch('statuses', function(newVal, oldValue) {
                  console.log(newVal, oldValue);
                  if (oldValue==newVal) return false;

                  console.log("st changed");
                },true);


                setTimeout(function() {
                    $scope.statuses.push("ahhh");
                    $scope.$apply();
                },3000);

$apply function serves for notification of update to $scope.

live sample http://plnkr.co/edit/LUjESyZOPfIALE2q5YYi?p=preview

if statuses will host just primitive values, better use $watchCollection

9 Comments

no primitive, it was just for ilustration but i dont understand why to use watch when i still have to use $apply... ?
difference between $watch and $watch collection, well documented here bennadel.com/blog/… $apply you have to use each time when you produce new scope. making a function for example. as function itself creates a scope(context)
missunderstanding :) My point is... i don't need to use watch if i use $apply right? The watch is there only if i need to do something when watched object changes?
nope. $apply is just bridge for you from your timeout function(where you did some changes) to watch callback(where you catch changes). So, you have to use both. Or as guys mentioned in other answer. $timeout. is another option. )
Hmmm... but then i should write something in the watch function what will update my scope or...? Because when i do this $scope.statuses.push("ahhh"); $scope.$apply(); it will automaticly update my scope even without the watch function.
|
1

Use $timeout which is the Angular version of setTimeout. When you use $timeout, AngularJS rebinds everything after the callback.

Having to manually call $apply is always a sign that you did not do something the angular way. It's a way to tell Angular: I've done something behind your back; please refresh.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.