Nasuh
Nasuh

Reputation: 355

AngularJS Factory Usage

.factory('MY', function($http){
return {
    mustafa: function(){
        var factory = {};
        var url = '/uzak/remote.php?callback=JSON_CALLBACK';
        var yarro = $http.get(url).success(function(response){
        return response.data);
        });
        return yarro;
    }
}
})
.controller('nbgCtrl', function() {
$scope.mangas = MY.mustafa();
 })

I wanna use json data above like. But it isn't working. Could you guys help me?

Upvotes: 0

Views: 50

Answers (1)

A. Rodas
A. Rodas

Reputation: 20679

You can return the promise, and then resolve it in the controller:

.factory('MY', function($http){
  return {
    mustafa: function() {
        var url = '/uzak/remote.php?callback=JSON_CALLBACK';
        return $http.get(url);
    }
  };
})

Finally, you have to inject the service to the controller.

.controller('nbgCtrl', function($scope, MY) {
  MY.mustafa().success(function(response) {
    $scope.mangas = response.data;
  );
});

Upvotes: 2

Related Questions