6

Sto provando a creare un servizio per ottenere json e passarlo a me homeCtrl Posso ottenere i dati ma quando lo passo al mio homeCtrl restituisce sempre undefined. Sono bloccato.AngularJS: restituire i dati dal servizio al controller

mio servizio:

var myService = angular.module("xo").factory("myService", ['$http', function($http){ 
    return{ 
    getResponders: (function(response){ 
     $http.get('myUrl').then(function(response){ 
     console.log("coming from servicejs", response.data); 
     }); 
    })() 
    }; 
    return myService; 
    } 
]); 

mio controller casa:

var homeCtrl = angular.module("xo").controller("homeCtrl", ["$rootScope", "$scope", "$http", "myService", 
function ($rootScope, $scope, $http, myService) { 
$scope.goData = function(){ 
    $scope.gotData = myService.getResponders; 
}; 
console.log("my service is running", $scope.goData, myService); 
}]); 

risposta

18

Si dovrebbe tornare promessa getResponders funzione, & quando viene risolto deve restituire response.data da quella funzione.

fabbrica

var myService = angular.module("xo").factory("myService", ['$http', function($http) { 
    return { 
     getResponders: function() {  
      return $http.get('myUrl') 
      .then(function(response) { 
       console.log("coming from servicejs", response.data); 
       //return data when promise resolved 
       //that would help you to continue promise chain. 
       return response.data; 
      }); 
     } 
    }; 
}]); 

anche dentro il controller si dovrebbe chiamare la funzione di fabbrica e utilizzare .then funzione per ottenere chiamarla quando la funzione getResponders servizio risolve la chiamata $http.get e assegnare il data a $scope.gotData

Codice

$scope.goData = function(){ 
    myService.getResponders.then(function(data){ 
      $scope.gotData = data; 
    }); 

}; 
+1

E se io volessi '$ scope.goData' per restituire i dati? Perché se inserisco un'altra variabile all'interno di $ scope.goData e poi la rendo uguale a data o $ ambito.gotData, sarebbe stata restituita prima che ottenesse il valore –

+0

Non so come funziona questa risposta, l'ho usata e non viene restituito nulla nel controller [Gist] (https://gist.github.com/wsfuller/4ca2b11748279a16c64dba0cb8032f81) –

+1

Ehi amico, grazie per l'attenzione .. Ho corretto il mio codice ora .. si prega di controllare aggiornato –

2

Questo è un esempio di come ho fatto per il mio progetto, funziona bene per me

var biblionum = angular.module('biblioApp', []);//your app 
 
biblionum.service('CategorieService', function($http) { 
 

 

 
    this.getAll = function() { 
 

 
     return $http({ 
 
      method: 'GET', 
 
      url: 'ouvrage?action=getcategorie', 
 
      // pass in data as strings 
 
      headers: {'Content-Type': 'application/x-www-form-urlencoded'} // set the headers so angular passing info as form data (not request payload) 
 
     }) 
 
       .then(function(data) { 
 

 
        return data; 
 

 

 
       }) 
 

 

 
    } 
 

 

 
}); 
 

 
biblionum.controller('libraryController', function($scope,CategorieService) { 
 
    
 
    var cat = CategorieService.getAll(); 
 
    cat.then(function(data) { 
 
     $scope.categories = data.data;//don't forget "this" in the service 
 
    }) 
 

 
    });

Problemi correlati