工場からデータJSONを取得しようとしています。json から何も得られなかったので、コンソールを見てみました。
angular.min.js:123 Possibly unhandled rejection: {"data":null,"status":-1,"config":{"method":"GET","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","url":"https://api.myjson.com/bins/d5nyl","headers":{"Accept":"application/json, text/plain, */*"}},"statusText":""}
この種のエラーについて教えてください
experience.html
<tr ng-repeat="experience in experiences" class="">
<td><p>{{experience.no}}</p></td>
<td><p>{{experience.name}}</p></td>
</tr>
(コントローラ)
angular.module('app').controller('experienceCtrl',['$scope','Experiences',function($scope,Experiences){
$scope.experiences= Experiences.get();
}]);
(サービス) experience.js
angular.module('app').factory('Experiences',['$http', function($http){
return{
get: function(){
return $http.get('https://api.myjson.com/bins/d5nyl').then(function(response){
return response.data;
});
}
};
}])
あなたのコードで$scope.experiences
は、 は promise オブジェクトでありreturn response.data;
、何も返しません。データを割り当てるには、コントローラーで promise オブジェクトを使用する必要があります。
angular.module('app')
.controller('experienceCtrl',
['$scope','Experiences',function($scope,Experiences){
Experiences.get()
.then(function(response) { $scope.experiences = response.data; });
}]);
angular.module('app').factory('Experiences',['$http', function($http){
return {
get: function(){
return $http.get('https://api.myjson.com/bins/d5nyl');
}
}
}]);
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加