6

Json'u almak ve bana iletmek için bir hizmet oluşturmaya çalışıyorumCtrl Verileri alabilirim ancak evime aktardığım zaman her zaman tanımsız olarak döner. Sıkıştım.AngularJS: hizmetten denetleyiciye veri döndürme

Benim Hizmeti:

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; 
    } 
]); 

My Home Denetleyici:

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); 
}]); 

cevap

18

buna o işlevinden response.data dönmelidir çözüme kavuşturulacağını zaman getResponders fonksiyonu & den söz dönmelidir.

Fabrika fabrika işlevini çağırmak ve getResponders hizmet fonksiyonu $http.get çağrısı giderir zaman diyoruz almak için .then işlevini kullanın ve data$scope.gotData için

atamak gerekir controller içerisinde Ayrıca

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; 
      }); 
     } 
    }; 
}]); 

Kod

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

}; 
+1

? Çünkü $ scope.goData içinde başka bir değişken başlatırsam ve veri veya $ scope.gotData'ya eşit yaparsam, vali –

+0

'u almadan önce döndürülürdüm Bu cevabın nasıl çalıştığından emin olmadı, onu kullandı ve hiçbir şey iade edilmedi denetleyiciye [Gist] (https://gist.github.com/wsfuller/4ca2b11748279a16c64dba0cb8032f81) –

+1

Hey dostum, başınız için teşekkürler .. Kodumu şimdi düzeltdim .. lütfen güncelleyin –

2

Bu örnek Projem için, bu benim için iyi çalıştınız nasıl ben verileri döndürmek için `$ scope.goData` istediği ne olur

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 
 
    }) 
 

 
    });

İlgili konular