2017-10-20 10 views
0

から返された私は、以下の方法で取得しています。この配列を持っています。 http post urlメソッドを使用して別のurlにポストします。私が悩んでいるのは、非同期呼び出しのコンセプトです。そのため、お気に入りの番号リストを取得して編集できるようにはなりません。助けてください!Angularjsは編集アレイは、HTTP GET URL

次のように私は約束を使用する方法を試してみました:「が定義されていないD」

angular.module('JuryApp').controller('mycontroller', ['myService', function (myService) { 

     myService.async(url).then(function(d) { 
    $scope.data = d; 
}); 

    app.controller('MainCtrl', function(myService,$scope) { 
    // Call the async method and then do stuff with what is returned inside  our own then function 
    myService.async().then(function(d) { 
    $scope.data = d; 
    }); 
}); 

をしかし、私はエラーを取得しておいてください。私がやっている私のコントローラで

 app.factory('myService', function($http) { 
     var myService = { 
     async: function(url) { 
     var promise = $http.get(url).then(function (response) { 
      console.log(response); 
      return response.data; 
     }); 
     // Return the promise to the controller 
     return promise; 
    } 
}; 

    return myService; 
}); 

を。デバッガが無限ループや何かに入る何らかのエラーが発生し続けます。

答えて

0

あなたはそれをオーバーコンプリートしていると思います。非同期呼び出しは実際にはかなり簡単です:

あなたがしているサービス:

app.factory("myService", ["$http", function($http) { 
    var MyService = { 
    getData: function(url) { 
     return $http.get(url); //$http returns a promise by default 
    } 
    }; 
    return MyService; 
})]; 

あなたのコントローラ:あなたがする必要があることすべてだ

angular.module('JuryApp').controller('mycontroller', ['myService', function (myService) { 
    $scope.FavNumbers = []; 
    var url = "http://my.api.com/"; 
    myService.getData(url).then(function(response) { 
    $scope.FavNumbers = response.data[0].FavNumbers; 
    }); 
}]);