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

标签 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 标签中打印那些数组元素。

最佳答案

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

将响应值分配给 Angular 范围变量以在 html 页面中访问该值。

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

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

关于javascript - 我如何使用 ng-repeat 迭代这个数组对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43448774/

相关文章:

javascript - 如何使用node.js和express读取post请求负载?

angularjs - 创建可重用的 AngularJS 提供者和服务

javascript - 如何设置数组中推送对象的属性?

javascript - 在 Chrome 控制台中分组相似

php - 如何查看用户使用的浏览器(和版本)?

javascript - AngularJS - 下拉刷新模块不起作用

c++ - 在构造函数中传递指向数组的指针

c - Eclipse 在分配 2D 数组元素期间无消息终止

javascript - 将带有事件监听器的元素传递给模板事件内的appendChild

javascript - 在 Angularjs ui-router 中获取以前的位置