【问题标题】:AngularJS - JSON data not showing in ng-repeat but does in console logAngularJS - JSON 数据未显示在 ng-repeat 中,但显示在控制台日志中
【发布时间】:2015-09-16 12:49:00
【问题描述】:

我在通过 API 调用显示我的 JSON 数据时遇到了困难,但如果我在控制台记录它,我可以看到响应。

我的应用是这样构建的

angular.module('myApp', ['angularSoundManager', 'infinite-scroll'])
.controller('MovieController', function($scope, $http){

$scope.songs = [];
$scope.data = $scope.songs.slice(0, 20);
$scope.getMoreData = function () {
$scope.data = $scope.songs.slice(0, $scope.data.length + 10);
};

获取数据的函数

function fetch(){
$http.get("http://www.someAPI.com" + $scope.search + "&perpage=150")
.success(function(response){
 $scope.details = response;
 console.log(response);

 // for loop happens here to loop through the data and push it to the empty array

  for(var i=0; i < response.tracks.length; i++){

    var allTracks = {
    title: response.tracks[i].primary_title,
    };

 };
 $scope.songs.push(allTracks);
}

我的 HTML 是

<div infinite-scroll='getMoreData()' infinite-scroll-distance='1'>
          <div ng-repeat="song in data">
           {{song.title}}
          </div>
        </div>

我可以看看我是否console.log(response) 我得到Object {ID: 0, tracks: Array[150], total: 257}

另一方面,如果我改用ng-repeat="song in songs",我可以显示我的数据。这与我将$scope.data 分配给$scope.songs 有关

我希望它与切片一起使用的原因是能够使用无限滚动,因为我的页面上可以预期 100 到 15.000 个结果

【问题讨论】:

  • 你的getMoreData 函数总是返回前十首歌曲。
  • 是的,当我搜索曲目时,它应该显示前十首歌曲,然后当我调用该函数时,它应该再添加 10 首歌曲。奇怪的是,当我将每页的结果限制为 30 条时,它就起作用了。就像它无法处理超过 100 个结果一样。
  • 那么您在页面上实际看到的是什么?
  • 如果我将结果限制在 100 以下,那么我会看到我想要的 .. 如果我尝试查看它,那么它不会在 ng-repeat div 中显示任何内容,但我仍然可以看到它正在被提取控制台日志并被推送到数组

标签: json angularjs angularjs-ng-repeat


【解决方案1】:

你可以通过设置 limitTo:100 来限制你的 ng-repeat

<div ng-repeat="s in song" | limitTo:100">
    {{s.title}}
</div>

【讨论】:

  • 这似乎没有任何好处,除了我不想限制结果。我希望所有结果都显示它们是否只有 10 或 15.000
  • 嗯,没错,没有正确阅读这个:) 检查这个答案then
【解决方案2】:

尝试使用

angular.copy(source,destination);

【讨论】:

    猜你喜欢
    • 2018-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-22
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多