2

Below is my Controller Code:

///////
//Diary Notes Controller
///////
diary.controller('NotesController', ['$scope','$http', 'notesService', function ($scope, $http, notesService){

//
//Get the Notes data back from the Server
//

$scope.updateFunc = function () {

    var notes = notesService.getText();

    console.log(notes);

};

$scope.updateFunc();

And my Services Code:

diary.factory('notesService', function ($http) {

return {

    getText: function () {

        $http.get('www/getNotes.php')
            .then(
            function (payload){
                return payload.data;
            });

    }

}

});

Basically When I do console.log the controller returns undefined for the notes variable which seems awkward as when i was using the controller to get the payload it works but returning the payload from the services doesn't seems to work.

4 Answers 4

2

$http.get is asynchronous function, that returns HttpPromise. There are couple ways how you can get data

1.Pass callback, like this

diary.factory('notesService', function($http) {
  return {
    getText: function (callback) {
      $http.get('www/getNotes.php')
      .then(
        function(payload) {
          callback(payload.data);
        });

    }
  }
});


notesService.getText(function (notes) {
  console.log(notes);
});

2.return promise

diary.factory('notesService', function($http) {
  return {
    getText: function () {
      return $http.get('www/getNotes.php');
    }
  }
});

notesService.getText().then(
    function(payload) {
        callback(payload.data);
    });
Sign up to request clarification or add additional context in comments.

Comments

2

You get undefined because you're not returning anything from getText(). Add a return statement before the $http call in your method:

getText: function () {
    return $http.get('www/getNotes.php')
        .then(function (payload) {
            return payload.data;
         });
}

Afterwards call the then method of the promise to get the value:

notesService.getText().then(function(notes) {
    console.log(notes);
});

2 Comments

Thanks for the answer .. The return was what I needed to make it work.. Now it returning promise value in Controller .. :) Thank You
You, sir, save my day.
1

$http.get returns a Promise.

Since the promise callback in then is async, you need to handle the promise in the controller. To do this, first return the promise in the factory:

return $http.get('www/getNotes.php')  <-- return added at the beginning of this line
.then(function (payload){
    return payload.data;
});

And then, handle the promise in the controller:

$scope.updateFunc = function () {   
    notesService.getText().then(function(notes) {     
        console.log(notes);
    });;
};

3 Comments

Can promise be handled in factory instead of controller??
What do you mean by handled? It is async.
I mean if I can process the returned values and add it into an array .etc
1

the problem is that you are not returning anything on the getText function. If you return the promise from the $http.get the you should implement any of the promise methods to fulfill your model variable notes

  diary.factory('notesService', function ($http) {

    return {

        getText: function () {

            return $http.get('www/getNotes.php')
                .then(
                function (payload){
                    return payload.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.