YardenST'in belirttiği gibi, $http
, eşzamanlı değildir, bu nedenle $http.get()
tarafından döndürülen verilere bağlı olan tüm işlevlerin veya görüntüleme mantığının buna uygun şekilde ele alındığından emin olmanız gerekir. Bunu yapmanın bir yolu, "vaadi" diye $http
döner faydalanmak için:
Plunkr Demo
var myApp = angular.module('myApp', []);
myApp.factory('AvengersService', function ($http) {
var AvengersService = {
getCast: function() {
// $http returns a 'promise'
return $http.get("avengers.json").then(function (response) {
return response.data;
});
}
};
return AvengersService;
});
myApp.controller('AvengersCtrl', function($scope, $http, $log, AvengersService) {
// Assign service to scope if you'd like to be able call it from your view also
$scope.avengers = AvengersService;
// Call the async method and then do stuff with what is returned inside the function
AvengersService.getCast().then(function (asyncCastData) {
$scope.avengers.cast = asyncCastData;
});
// We can also use $watch to keep an eye out for when $scope.avengers.cast gets populated
$scope.$watch('avengers.cast', function (cast) {
// When $scope.avengers.cast has data, then run these functions
if (angular.isDefined(cast)) {
$log.info("$scope.avengers.cast has data");
}
});
});
Hey, cevabım sonsuza kadar açık kalmaz böylece onu kabul edin sonra ne olduğunu ise . Şerefe! – GFoley83