【问题标题】:AngularJS ng-repeat not display my dataAngularJS ng-repeat 不显示我的数据
【发布时间】:2018-11-09 12:04:15
【问题描述】:

我的 ng-repeat 不显示我的数据。但在console.log 中显示正常。我正在使用带有后端的 laravel。我做错了什么?

<!DOCTYPE html>
<html ng-app="myApp">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>


<div ng-controller="myCtrl">
    <tr ng-repeat="t in teste">
        <td>{{t.id}}</td>
        <td>{{t.total}}</td>
    </tr>    
</div>


<script>


var app = angular.module('myApp', []);

app.controller('myCtrl', function($scope, $http) {
  $http.get("//localhost:8000/angular")
  .then(function(response) {        
      $scope.teste = response.data;
      console.log($scope.teste);



  });
});
</script>

</body>
</html>

【问题讨论】:

  • 请翻译成英文
  • 这个问题属于pt.stackoverflow.com
  • 这是一个英文网站,请将您的问题翻译成英文或发布到您的语言的交易所。
  • 我的 ng-repeat 不显示我的数据。但是在console.log中显示正常
  • @DiegoMarcelo &lt;tr&gt; 是表格的一部分,您需要用&lt;table&gt; 标签包装您的代码

标签: javascript angularjs laravel


【解决方案1】:

这是您的代码更新,我认为您只需将 tr 标签包装在 table 标签中。希望对你有帮助...

我还调用了伪造的数据 API,因为在您的示例中使用了 localhost 链接

<!DOCTYPE html>
<html ng-app="myApp">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<script>


var app = angular.module('myApp', []);

app.controller('myCtrl', function($scope, $http) {
  $http.get("https://jsonplaceholder.typicode.com/posts")
  .then(function(response) {    
  setTimeout(function () {
        $scope.$apply(function () {
           $scope.teste = response.data;
        });
    }, 0);
     

  });
});
</script>

<div ng-controller="myCtrl">
<table >
  <tr style="text-align : left">
    <th>Id</th>
    <th colspan="2">Title</th>
  </tr>
  <tr ng-repeat="t in teste track by $index">
        <td>{{t.id}}</td>
        <td>{{t.title}}</td>
    </tr> 
</table>
       
</div>
</body>
</html>

【讨论】:

    【解决方案2】:

    这个帖子是很久以前写的,所以这是给在谷歌搜索中偶然发现这个帖子的人。我犯了一个错误,没有使用*ngFor 而不是ng-repeat,在最新版本的Angular 中没有使用。我阅读了文档来解决这个问题:https://angular.io/guide/ajs-quick-reference

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-28
      • 2016-09-06
      • 2020-08-16
      • 1970-01-01
      • 2016-02-22
      • 1970-01-01
      相关资源
      最近更新 更多