2016-01-13 77 views
-3

我想环路上<div id="product">但不能算不好用,我想改变示例数据fakeModel JSON的更多细节这里是preview,也许能帮助我,,如何循环的Json随着div元素在角JS

fakeModel = { 

    id: 1, 

    title: 'product', 

    description: 'description', 

    price: 10, 

    tax: 0, 

    vendorFees: 0, 

    qty: 0 

}; 
+0

我看到你有你的数据在json文件中。使用'$ http.get'获取它并使用'ng-repeat'遍历它 – user7

+0

你能举个例子吗? – Ivisivel

+0

[Plunkr](http://plnkr.co/edit/fDc5ZE0mwqoaIZfowxgA?p=preview)我对控制器中的'products.json'进行了硬编码,以演示ng-repeat。 – user7

回答

1

要通过角列表迭代你可以尝试这样的事情:

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

给ID来添加的div

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

你能举一个例子吗?产品中的产品如何工作? – Ivisivel

+0

[使用产品中的产品](http://plnkr.co/edit/13BoagVBkK9G27IHQu74?p=preview) – tpsilva

+0

是的,现在我知道ng-repeat,但无法区分id 1,2,3是增加还是减少购物车 – Ivisivel

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> 
+0

我可以区分一下id 1,2, 3在我的例子中增加或减少购物车? – Ivisivel

+0

是的,你区分ID。 –