angular.js - json对象可以使用ng-repeat吗

浏览:36日期:2023-01-04

问题描述

一个jsonobject, 希望可以遍历获取key和value, 是否可以用ng-repeat的方式获取,或者别的简单的方式,达到类似以下的效果;以下jsonobject使用ng-repeat会报错

$scope.data = {’aaa’ : ’123’ , ‘bbb’:’456’,’ccc’:’789’}<p ng-repeat='m in data'> key = {{m.key}} value = {{m.value}}</p>

问题解答

回答1:

<p ng-repeat='(key, val) in data'> key = {{key}} value = {{val}}</p>回答2:

可以参照一下angular官方对ngRepeat的讲解:

Iterating over object propertiesIt is possible to get ngRepeat to iterate over the properties of an object using the following syntax:

<p ng-repeat='(key, value) in myObj'> ... </p>

相关文章: