简体   繁体   English

Angularjs $ http.get()。 JSON fron服务器

[英]Angularjs $http.get(). JSON fron server

I try to show part of server list, but does not understand why he had not written them to the array.I try to push it to array and then display it in html using ng-repeat, but it's doesn't work as it I have not tried. 我尝试显示服务器列表的一部分,但不明白为什么他没有将它们写到array.I尝试将其推送到array,然后使用ng-repeat将其显示在html中,但由于它不起作用没有尝试过。

var app = angular.module('myApp', []);
app.factory("demoFac", ['$http',function($http){
    var obj = {};
    for(id = 10; id < 16; id++){
        obj.fetchUserDetails = function(){
            return $http.get('http://jsonplaceholder.typicode.com/photos/' + id);
        }
        return obj;
    }

}]);

app.controller('customersCtrl',function(demoFac){
    var Picture = this;

    Picture.list = [];

    demoFac.fetchUserDetails().success(function(response){
        Picture.list.push({id: response.id,title:response.title, url: response.url, thumbnailUrl: response.thumbnailUrl})
    });

    console.log(Picture.list);
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="customersCtrl as Picture" ng-cloak> 
    <div ng-repeat="x in    Picture.list ">
    <h4>{{'id:'+x.id}}</h4>

    <h3>{{x.title}}</h3>
    <h4>{{x.url}}</h4>
    <h4>{{x.thumbnailUrl}}</h4>
    <img ng-src="{{x.thumbnailUrl}}">
    <img ng-src="{{x.url}}">
</div>
</div>

I tried different ways but the result is the same, do not understand what I amas doing wrong ? 我尝试了不同的方法,但结果是一样的,不明白我amas做错了什么吗?

Its not empty actually , If you see the network tab the response is coming with 1 element.. its showing empty because by the time success callback is called the console.log statement is already getting executed with initial value as []. 它实际上不是空的,如果您看到网络选项卡,响应将带有1个元素。它显示为空,因为到成功调用回调时, console.log语句已经被执行,初始值为[]。

I know then why it showed 1 element first time , well because believe it or not the chrome's developers tool's console behaves bit differently, it evaluates your logs after you open them or something like that. 我知道然后为什么它第一次显示1个元素 ,好吧,因为不管您相信chrome的开发人员工具的控制台的行为是否有所不同,它都会在打开日志或类似的东西后评估您的日志。 I have came across with same question before and found this thing in one of the answer. 我之前也遇到过同样的问题,并且在答案之一中发现了这个问题。

Just for proof run this in Firefox and you would always find the log as empty array. 为了证明这一点,请在Firefox中运行它,您总是会发现日志为空数组。

This code will solve your issue: 此代码将解决您的问题:

 var app = angular.module('myApp', []); app.factory("demoFac", ['$http', '$q', function($http, $q) { var obj = {}; obj.fetchUserDetails = function() { var promises = []; for (id = 10; id < 16; id++) { var promiss = $http.get('http://jsonplaceholder.typicode.com/photos/' + id); promises.push(promiss); } return $q.all(promises); } return obj; } ]); app.controller('customersCtrl', ['demoFac', function(demoFac) { var Picture = this; Picture.list = []; demoFac.fetchUserDetails().then(function(response) { for (var i = 0; i < response.length; i++) { Picture.list.push({ id: response[i].data.id, title: response[i].data.title, url: response[i].data.url, thumbnailUrl: response[i].data.thumbnailUrl }); } }); console.log(Picture.list); } ]); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="myApp" ng-controller="customersCtrl as Picture" ng-cloak> <div ng-repeat="x in Picture.list "> <h4>{{'id:'+x.id}}</h4> <h3>{{x.title}}</h3> <h4>{{x.url}}</h4> <h4>{{x.thumbnailUrl}}</h4> <img ng-src="{{x.thumbnailUrl}}"> <img ng-src="{{x.url}}"> </div> </div> 

You were setting the fetchUserDetails to only fetch the last item (with id: 15) because you were resetting the obj.fetchUserDetails over and over again in for loop. 您将fetchUserDetails设置为仅获取最后一个项目(ID为15),因为要在for循环中一次又一次地重置obj.fetchUserDetails。 What I did was to move the for loop inside fetchUserDetails and collect all promises for each call into a single promise by $q.all(). 我所做的是将fetchUserDetails中的for循环移动到了,并通过$ q.all()将每个调用的所有promise收集到一个promise中。 The new unified promise has been used in customersCtrl. 新的统一承诺已在客户Ctrl中使用。 The unified promise will work once all promises has been resolved. 一旦所有的承诺都得到解决,统一的承诺就会生效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM