我如何使用ng-repeat迭代这个数组对象

时间:2017-04-17 09:30:55

标签: javascript angularjs arrays angular-directive

我在控制台中返回了以下对象数组现在我想要做的是在我的index.html文件中使用ng-repeat迭代它,所以我在这里做的是返回的json: -

{body: ["xx", "x", "x", "vinit↵", "gyghg", "hjhghg", "j", "jj", "bn", "ss"]}

我的app.js: -

var app = angular.module('myApp',[]);
app.controller('movie',['$http','$scope',function($http,$scope) {
    $scope.products = [];
    $http.get('blog/').then(successCallback, errorCallback);
    function successCallback(response){
        products=response.data;
        console.log(products);
    }
    function errorCallback(error){
        console.log(error);
    }

    // body...
}]);

这里我正在做什么来迭代我的index.html中的那些对象: -

<div ng-repeat="item in products">
    <ul>
        <li>{{item.body}}</li>
    </ul>
</div>

但它不在屏幕上打印任何我想要的东西就是在那个li标签中打印那些数组元素。

6 个答案:

答案 0 :(得分:1)

你应该有一个$ scope变量产品,

更改

发件人

 products=response.data;

 $scope.products=response.data;

你应该能够访问,

<div ng-repeat="item in products">
    <ul>
        <li>{{item}}</li>
    </ul>
</div>

答案 1 :(得分:1)

products=response.data;更改为$scope.products=response.data;

将{{item.body}}更改为{{item}}

<div ng-repeat="item in products">
    <ul>
        <li>{{item}}</li>
    </ul>
</div>

答案 2 :(得分:1)

试试这个

&#13;
&#13;
<!DOCTYPE html>
<html>
<head>
  <title></title>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.11/angular.min.js"></script>
  <script type="text/javascript">
    angular.module("app", []).controller('Controller', Controller);

    function Controller($scope, $http) {
      var res =  {body: ["xx", "x", "x", "vinit↵", "gyghg", "hjhghg", "j", "jj", "bn", "ss"]};
      $scope.products = res.body;
    }

  </script>
</head>
<body>

  <div ng-app="app" ng-controller="Controller" class="container">
    <div ng-repeat="item in products track by $index">
      <ul>
        <li>{{item}}</li>
      </ul>
    </div>
  </div>
</body>
</html>
&#13;
&#13;
&#13;

答案 3 :(得分:1)

需要将products=response.data;更改为$scope.products=response.data;

将响应值分配给angular scope变量以在html页面中访问该值。

将{{item.body}}更改为{{item}}

    <li>{{item}}</li>

答案 4 :(得分:1)

每个人都告诉你,你需要更换

SELECT GRP_ID,ID, (SELECT Max(ID)  FROM TEST A WHERE A.ROWID<>B.ROWID AND A.GRP_ID=B.GRP_ID) maX_ID FROM TEST B;

products=response.data; 

之后,在视图中,您想迭代body元素,所以:

 $scope.products=response.data;

答案 5 :(得分:0)

试试这个

var app = angular.module('myApp',[]);
app.controller('movie',['$http','$scope',function($http,$scope) {
$scope.products = [];
$http.get('blog/').then(successCallback, errorCallback);
function successCallback(response){
    $scope.products=response.data;
    console.log($scope.products);
}
function errorCallback(error){
    console.log(error);
}

// body...
}]);