I have an object in a service which is loaded asynchronously from a PouchDB.
I cannot in any way get the view to update as the service object is updated. I have followed many tutorials, answers, plnkrs, gists and just can't get it.
See http://plnkr.co/edit/jtFNs5FbrDBLlyZ5TATn?p=preview for my example. Any ideas?
Controller code:
var hubApp = angular.module('hubApp', []);
hubApp.service('objService', function($q, $timeout) {
var objects = [];
this.getObjects = function() {
var data = $q.defer();
var foo = data.promise;
$timeout(function() {
data.resolve([{"name": "first"},{"name": "second"}]);
}, 2);
foo.then(function(objs){
console.log(objs);
angular.forEach(objs, function(obj) {
objects.push(obj);
})
console.log(objects);
});
}
this.getObjects();
});
hubApp.controller('ListCrtl', ['$scope', 'objService',
function ($scope, objService) {
$scope.objects = objService.objects;
}
]);
View code:
<body ng-controller="ListCrtl">
<p ng-repeat="item in objects">Hello {{item.name}}!</p>
</body>
Thanks.
var objectsis not same asthis.object. Why cant you callgetObjectsand chain it through the promise. How will you make sure that by the time the controller is invoked the promise would have resolved.