如何在角度JS中使用Div元素循环Json

时间:2016-01-13 16:14:27

标签: angularjs json

我想循环<div id="product">但不算不行,我想用json更改示例数据fakeModel更多详细信息,这里是preview,也许可以帮助我,

fakeModel = {

  id: 1,

  title: 'product',

  description: 'description',

  price: 10,

  tax: 0,

  vendorFees: 0,

  qty: 0

};

2 个答案:

答案 0 :(得分:2)

要以角度迭代列表,您可以尝试这样的事情:

<div ng-repeat="product in products">
    <div>{{product.title}}</div>
    <div>{{product.price}}</div>
</div>

为添加的div提供ID

<div id="product_{{product.id}}" ng-repeat="product in products">
    <div>{{product.title}}</div>
    <div>{{product.price}}</div>
</div>

答案 1 :(得分:0)

请试试这个。

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
    <div ng-app="myApp" ng-controller="namesCtrl">      

        <div ng-repeat="product in fakeModel">
            <div>Product name : {{product.title}}</div>
            <div>Product price : {{product.price}}</div>
        </div>  
    </div>

        <script>
            angular.module('myApp', []).controller('namesCtrl', function($scope) {
                $scope.fakeModel = [{
                      id: 1,
                      title: 'product',
                      description: 'description',
                      price: 10,
                      tax: 0,
                      vendorFees: 0,
                      qty: 0
                    },
                    {
                      id: 2,
                      title: 'product 2',
                      description: 'description',
                      price: 15,
                      tax: 0,
                      vendorFees: 0,
                      qty: 0
                    }];
            });
      </script>