Return value of a promise to a function in AngularJS -


i have problem outputting values of promise function.

$scope.getteacher = function(teacherabr) {   var promise = dataservice.getteacher(teacherabr);    var testje = promise.then(function(payload) {     return payload;   });    return testje; // outputs d object, not direct payload values }; 

the output of controller function this: output of function

however, when i'm doing testje.$$state returns undefined. how can output value object? can't output payload variable in new $scope, because data dynamic.

here simplified version of on plunker.

you should change way think things when work asynchronous code. no longer return values, instead use promise or callback patterns invoke code when data becomes available.

in case factory can be:

.factory('dataservice', function($http, $log, $q) {     return {         getteacher: function(teacher) {             // jsonp request ('/teacher/' + teacher)             return $http.get('http://echo.jsontest.com/key/value/one/two').then(function(response) {                 return response.data;             }, function() {                 $log.error(msg, code);             })         }     }; }); 

and usage in controller:

dataservice.getteacher('lanc').then(function(data) {     $scope.teacher = data; }); 

also $http.get returns promise, don't have create 1 more $q.defer().

demo: http://plnkr.co/edit/fnymzg9njr7gpjgkkwd6?p=preview

upd

here effort combining lessons teachers.

demo: http://plnkr.co/edit/sxt5qaizuiqgze2z6pb4?p=preview


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -