【问题标题】:ng-repeat isn't showing json object itemsng-repeat 不显示 json 对象项
【发布时间】:2023-03-09 22:45:01
【问题描述】:

我想知道为什么我的项目没有显示在 DOM 上。尽管 ng-repeat 正在制作新的表格行。当我控制台日志簿时,它显示得很好,但不是ng-repeatConsole Log

HTML

<table class="table table-bordered table-light table-hover">
<tbody  id="tableBookSearch">
<tr ng-repeat="(key, value) in books"><td>key : {{key}} value: {{value}}</td></tr>
</tbody>
</table>

JS

$.getJSON(googleAPI, function(response){
        $scope.books = [];
        for(var i = 0; i < response.items.length; i++){
          $scope.books.push(response.items[i]);
          console.log('pushed book : ' + i);
        }
        $scope.$apply();
        console.log('scope applied');
        console.log($scope.books);
});

更新:

我尝试使用普通数组执行此操作,但它也没有显示数据。引用也不为空。

【问题讨论】:

  • 请同时包含console.log 的输出。
  • 如果你console.log()你的response你会得到什么?
  • 添加您的控制器代码。
  • 你为什么在这里使用push?您可以只使用$scope.books= response.items; 而不使用for loop
  • @RameshRajendran $scope.books = response.items;确实有效,但是我只是使用循环来确保我们得到正确的返回和正确的绑定。

标签: javascript angularjs json


【解决方案1】:
<table class="table table-bordered table-light table-hover">
<tbody  id="tableBookSearch">
<tr ng-repeat="(key, prop) in books">
  <td>   Key : {{key}}</td> 
   <td ng-repeat="value in books[key]>  value : {{value }}</td>
</tr>
</tbody>
</table>


https://www.w3schools.com/angular/ng_ng-repeat.asp

【讨论】:

    【解决方案2】:

    而不是使用 ng-repeat go for ng-options in angular find @https://docs.angularjs.org/api/ng/directive/ngOptions

    【讨论】:

    【解决方案3】:

    我正在编码的环境是 Angular2,我认为它是 AngularJS。 ng-repeat 是 AngularJS 的指令,要执行类似的操作,我必须使用 *ngfor。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-04
      • 1970-01-01
      • 2015-08-06
      相关资源
      最近更新 更多