I'm very new to AngularJS, and I apologize if this is a silly question.  

In developing a simple app, I ran across an issue that didn't make any 
sense to me, and I boiled it down to a simple test example that is shown 
below.

I'm simply binding to a global variable, then I try to modify it in an 
anonymous function called by setTimeout.  My first question is that I 
really don't understand why this doesn't work, but perhaps more concerning 
to me is that it does work if I use the $q service to wrap it in a promise. 
 If anyone would care to please explain to me what I'm not getting about 
this, I would very much appreciate it.


*HTML*

 

<div ng-controller="TestController">{{ data }}</div>


*JAVASCRIPT*


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

var globalData = ["first","second","third"];

app.controller('TestController', function ($scope, $q) {
    $scope.data = globalData;

    //// DATA DOES NOT UPDATE
    // setTimeout(function() {
    //     globalData.push("fourth");
    // }, 2000);

    //// WORKS FINE - DATA IS UPDATED
    (function() {
        var d = $q.defer();
        setTimeout(function() {
            globalData.push("fourth");
            d.resolve();
        }, 2000)
        return d.promise;
    })().then(function(){});
});

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to