2

I have a problem with outputting the values of a promise function.

$scope.getTeacher = function(teacherAbr) {
  var promise = dataService.getTeacher(teacherAbr);

  var testje = promise.then(function(payload) {
    return payload;
  });

  return testje; // outputs an d object, not the direct payload values
};

The output of this controller function is this: output of function

However, when I'm doing testje.$$state it returns undefined. How can I output the value object? I can't output the payload variable in a new $scope, because this data is dynamic.

Here is a simplified version of this on Plunker.

2 Answers 2

6

You should change the way you think about things when you work with asynchronous code. You no longer return values, instead you use Promise or callback patterns to invoke code when data becomes available.

In your case your factory can be:

.factory('dataService', function($http, $log, $q) {
    return {
        getTeacher: function(teacher) {
            // Originally this was a 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 already returns promise, so you don't have to create one more with $q.defer().

Demo: http://plnkr.co/edit/FNYmZg9NJR7GpjgKkWd6?p=preview

UPD

Here is another effort for combining lessons with teachers.

Demo: http://plnkr.co/edit/SXT5QaiZuiQGZe2Z6pb4?p=preview

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, but how can I use this in a template with ng-repeat? There are multiple teachers in the ng-controller. So I need something like this: <div ng-repeat="teacher in getTeacher(lesson.teacher)"></div>
You simply use $scope.teachers object in template: <div ng-repeat="teacher in teachers)"></div>.
That's not the problem unfortunately, I've made a better demo to illustrate it. Thank you very much for helping me btw :).
I see what your problem is.. Then it becomes more complicated. See this demo.
You can't achieve it like your are trying to do it. So you need to combine teacher info with lesson outside of ngRepeat. I would recommend to make your webserver do it: it's very easy with LEFT JOINS, etc. Or you can do it like I did in another demo. See updated answer.
|
0

//in your services file

  return {
      getTeacher: function (teacher) {
        // Originally this was a jsonp request ('/teacher/' + teacher)
       return $http.get('http://echo.jsontest.com/key/value/one/two')

        })

//change the controller to this

dataService.getTeacher(teacherAbr).then(function(msg){

     $scope.getdata=msg.data;
      console.log("From server"+msg.data);

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.