angularjs – ng-repeat:来自JSON数组对象的访问键和值
发布时间:2020-12-17 08:40:42 所属栏目:安全 来源:网络整理
导读:我有JSON Array对象如下所示。 $scope.items = [ {Name: "Soap",Price: "25",Quantity: "10"},{Name: "Bag",Price: "100",Quantity: "15"},{Name: "Pen",Price: "15",Quantity: "13"}]; 我想使用ng-repeat在angular.js中单独获取键和值。我试过下面的代码,
我有JSON Array对象如下所示。
$scope.items = [ {Name: "Soap",Price: "25",Quantity: "10"},{Name: "Bag",Price: "100",Quantity: "15"},{Name: "Pen",Price: "15",Quantity: "13"} ]; 我想使用ng-repeat在angular.js中单独获取键和值。我试过下面的代码,但它不工作。 <tr ng-repeat="(key,val) in items"> <td>{{key}}</td> <td>{{val}}</td> </tr> 我相信问题是用大括号’和’]。任何人都可以建议我如何解决这个问题? 编辑: 非常感谢你的答复。我已经尝试过你的代码和它的工作。但我的真正需求是显示项目如下所示。 Name Price Quantity Soap 25 10 Bag 100 15 Pen 15 13 我使用一些< tr>和< td>在html。但没有得到displayd在屏幕上。代码如下所示。 <table> <tr ng-repeat="item in items"> <tr ng-repeat="(key,val) in item"> <td>{{key}}</td> <td>{{val}}</td> </tr> </tr> </table> 我知道< tr>在另一个< tr> html不允许。我试过best.But没有运气。
你有一个对象数组,所以你需要使用ng-repeat两次,如:
<ul ng-repeat="item in items"> <li ng-repeat="(key,val) in item"> {{key}}: {{val}} </li> </ul> 示例:http://jsfiddle.net/Vwsej/ 编辑: 请注意,不保证对象中的属性顺序。 <table> <tr> <th ng-repeat="(key,val) in items[0]">{{key}}</th> </tr> <tr ng-repeat="item in items"> <td ng-repeat="(key,val) in item">{{val}}</td> </tr> </table> 示例:http://jsfiddle.net/Vwsej/2/ (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |