AngularJS ng-repeat不显示我的数据

时间:2018-05-30 15:03:40

标签: javascript angularjs laravel

我的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>

2 个答案:

答案 0 :(得分:0)

这是您的代码更新,我认为您只需要在表标记中包含tr标记。希望它有所帮助...

我还调用假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>

答案 1 :(得分:0)

该线程是在一段时间之前创建的,所以这是给那些偶然在Google搜索中偶然发现的人使用的。我犯了一个错误,那就是没有使用$channel->reconnect(); 而不是*ngFor,而在最新版本的Angular中却没有使用。我阅读了文档以找出答案:https://angular.io/guide/ajs-quick-reference