0

I am trying to create factory and use it in the controller, the factory return data from get method and save it in the controller but its not working, and $scope.myData return undefind.

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, myService) {
    $scope.myData = myService.getEvent();
});
app.factory('myService', function($http){
    var oGetData = {};
    oGetData.getEvent = function(){
        $http.get('http://citysdk.dmci.hva.nl/CitySDK/events/search?category=festival')
        .then(function(response) {
         return response.data.event;
        });
    };

    return oGetData ;
});

when i use factory code directly in the controller its work fine

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
    $http.get('http://citysdk.dmci.hva.nl/CitySDK/events/search?category=festival')
        .then(function(response) {
            $scope.myData = response.data.event;
        });
});

can someone tell me what i did wrong in the first code please?

here is the codepen http://codepen.io/anon/pen/NRVZdE

1 Answer 1

1

Working Codepen: http://codepen.io/jdoyle/pen/KgLjgY

This is a commonly asked question. You are expecting this to return data, but it does not:

app.controller('myCtrl', function($scope, myService) {
    $scope.myData = myService.getEvent();
});

getEvent() returns a promise, not data. You need to treat the return object just like you would a call to $http:

app.controller('myCtrl', function($scope, myService) {
    myService.getEvent().then(function(response){
      $scope.myData = response.data.event;
    });
});

And in your factory just return the call to $http and nothing else:

oGetData.getEvent = function(){
    return $http.get('http://citysdk.dmci.hva.nl/CitySDK/events/search?category=festival');
};

If you want to modify the data before it comes back to the controller, you can create your own deferred and handle the response yourself, like this:

oGetData.getEvent = function(){
    var deferred = $q.defer();
    $http.get('http://citysdk.dmci.hva.nl/CitySDK/events/search?category=festival')
    .then(function(response) {
       deferred.resolve(response.data.event);
     });
    return deferred.promise;
};

Then you don't have to worry about getting the event from the response data:

app.controller('myCtrl', function($scope, myService) {
    myService.getEvent().then(function(event){
      $scope.myData = event;
    });
});
Sign up to request clarification or add additional context in comments.

3 Comments

this code output error TypeError: Cannot read property 'then' of undefined
Which line is it referring to? Do you have a plunkr or Codepen to look at?
You forgot to inject $q as a dependency in your factory. I should have mentioned that

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.