AngularJS转发器迭代次数过多

时间:2018-02-04 22:23:04

标签: angularjs

所以我得到了这个HTML:

<body ng-app="pastaApp">
<div class="container">
    <div class="row">
        <div ng-controller="dataService">
            <ul>
                <li ng-repeat="post in testAPI">{{post.title}}</li>
            </ul>
        </div>
    </div>
</div>
<script src="app.js"></script>

这在我的app.js中:

pastaApp.controller('dataService', function ($scope, $http) {
    $http({
      method: 'GET',
      url: 'https://jsonplaceholder.typicode.com/posts/100'
   }).then(function (success){
        $scope.testAPI = success;
   },function (error){
        //
   });
});

我怀疑它给了我一个李,但它给了我5分。 它给了我这个HTML:

<ul>
<!-- ngRepeat: post in testAPI -->
<li ng-repeat="post in testAPI" class="ng-binding ng-scope">at nam consequatur ea labore ea harum</li>
<!-- end ngRepeat: post in testAPI -->
<li ng-repeat="post in testAPI" class="ng-binding ng-scope"></li>
<!-- end ngRepeat: post in testAPI -->
<li ng-repeat="post in testAPI" class="ng-binding ng-scope"></li>
<!-- end ngRepeat: post in testAPI -->
<li ng-repeat="post in testAPI" class="ng-binding ng-scope"></li><!-- end ngRepeat: post in testAPI -->
<li ng-repeat="post in testAPI" class="ng-binding ng-scope"></li>
<!-- end ngRepeat: post in testAPI -->
<li ng-repeat="post in testAPI" class="ng-binding ng-scope"></li>
<!-- end ngRepeat: post in testAPI -->
</ul>

我得到太多循环。 可能有一个简单的修复,但我是新手。

我看过像ng-class这样的修补程序,但它不起作用。

2 个答案:

答案 0 :(得分:0)

确保&#39;成功&#39;是一个数组。您也可以将其实现为[success]。

答案 1 :(得分:0)

您已将$ scope.testAPI变量指定为字符串(成功)。 请使它成为一组对象来打印标题。 例如 $ scope.testAPI = [{title:'test 1'},{title:'test 2'}];