AngularJS : Service data binding -
i trying call service in angular.js through controller on load , return promise. expect promise fulfilled , dom updated. not happens. clear, not getting error. code follows.
app.controller('tutorialcontroller', function ($scope, tutorialservice) { init(); function init() { $scope.tutorials = tutorialservice.gettutorials(); } });
<div data-ng-repeat="tutorial in tutorials | orderby:'title'"> <div>{{tutorial.tutorialid}}+' - '+{{tutorial.title + ' - ' + tutorial.description}}</div> </div>
var url = "http://localhost:8080/tutorial-service/tutorials"; app.service('tutorialservice', function ($http, $q) { this.gettutorials = function () { var list; var deffered = $q.defer(); $http({ url:url, method:'get' }) .then(function(data){ list = data.data; deffered.resolve(list); console.log(list[0]); console.log(list[1]); console.log(list[2]); }); return deffered.promise; }; });
inside of ".then()" function in service, log results , getting expected there, never updates dom. , appreciated.
gettutorials
returns promise itself. have then()
again.
tutorialservice.gettutorials().then(function(data){ $scope.tutorials = data; });
before that, $http
returns promise success()
, error()
.
although can use then
well
since returned value of calling $http function promise, can use method register callbacks, , these callbacks receive single argument – object representing response.
so correct that.
Comments
Post a Comment