Automatically updating scope variables in angularjs

I am playing with AngularJS now. I would like to return a variable from the service that will let the region know when it changed.

To illustrate this, take a look at the example from the website www.angularjs.org "Connect the backend." Roughly, we can see the following:

var projects = $firebase(new Firebase("http://projects.firebase.io"));
$scope.projects = projects;

After that, all updates made to the object projects(through updates, whether locally or remotely) will be automatically reflected in the view to which the area is attached.

How can I achieve the same in my project? In my case, I want to return the "self-update" variable from the service.

var inbox = inboxService.inboxForUser("fred");
$scope.inbox = inbox;

What mechanisms let $scopeknow that it should be updated?

EDIT: . :

$scope.auto = {
    value: 0
};

setInterval(function () {
    $scope.auto.value += 1;
    console.log($scope.auto.value);
}, 1000);

- :

<span>{{auto.value}}</span>

, 0. ?

+3
2

UPDATE:

: http://plnkr.co/edit/dmu5ucEztpfFwsletrYW?p=preview
$timeout .


javascript:

  • .
  • , .
  • , "".
  • $http, .
  • , , , , , .
  • AFAIK, $firebase Restangular.
  • , .
  • , .

:

:

app.factory('inboxService', function($http){

  return {
    inboxForUser: function(user){

      var inbox = {};

      $http.get('/api/user/' + user).then(function(response){
        angular.extend(inbox, response.data);
      })

      return inbox; 
    }
  };
});

:

app.controller('ctrl', function(inboxService){
  $scope.inbox = inboxService.inboxForUser("fred");
});
+9

, . "" angular, (. http://docs.angularjs.org/guide/scope), . Angular.

"" angular (, jQuery), , , , $apply. - :

$scope.$apply(function() {
    //my non angular code
});

. http://docs.angularjs.org/api/ng.$rootScope.Scope.

+4

All Articles