使用 ng-repeat 以特定键定位特定数据项

Targeting specific data items with a particular key with ng-repeat

我有一个 JSON 对象,如下所示:

{
   "name": "cameraasd_main_autofocus",
   "value": "Lasasd autofocus",
   "displayName": "Autofocus"
 },
{
   "name": "screen_siasdze",
   "value": "5.2\asd",
   "displayName": "Sdascreen Size",
   "group": "General"
},
{
   "name": "camera_maindas_features",
   "value": "Digital Zoom,das Auto Flash, Digital image stabilization"
   "displayName": "Features"
},

在这个数据对象中,我只想定位具有 group 属性和 ng-repeat 的元素。我必须进一步将元素分组到一个特定的组中。例如,从整个对象中,组值为 "General" 的元素应与其余元素分开。

<div ng-repeat="x in records">
    <div ng-if="x.group"> {{x.name}} </div>
</div>

试试这个是否有效!!祝你好运!

假设您的 JSON 包含一个数组(它没有显示在您的 json 中,但我假设它在那里)。 首先,将您的 JSON 解析为 JsonObject,然后将其作为数组

JSONArray jsonArray = new JSONArray(jsonString);

然后您可以在 html

中执行此操作
<div ng-repeat="elem in jsonArray">
   <a ng-if="elem.group">{{ elem.property }}</a>
</div>

您可以创建一个范围变量来存储具有 属性 "group" 的筛选项,如下所示

var data = [{
   "name": "cameraasd_main_autofocus",
   "value": "Lasasd autofocus",
   "displayName": "Autofocus"
 },
{
   "name": "screen_siasdze",
   "value": "5.2asd",
   "displayName": "Sdascreen Size",
   "group": "General"
},
{
   "name": "camera_maindas_features",
   "value": "Digital Zoom,das Auto Flash, Digital image stabilization",
   "displayName": "Features"
}];

$scope.filteredData = data.filter(function(x){ return x.hasOwnProperty("group")});

然后在UI

中使用ng-repeat
<ul>
    <li ng-repeat="item in filteredData">
      {{item.name}} || {{item.value}} 
    </li>
</ul>

这是一个工作示例:https://jsfiddle.net/Lt7aP/8267/

编辑:如果你想根据特定的组名分离出元素,那么你可以尝试下面的代码

$scope.filteredData = data.filter(function(x){ return x.hasOwnProperty("group") && x.group == "General"}); 

在这种情况下,将仅显示 General 个组。

检查此代码。

<div>
        <h3>With Group</h3>
        <div ng-repeat="d in data">
            <div ng-if="d.group">
                {{d.name}}
            </div>
        </div>
    </div>

    <div>
        <h3>Without Group</h3>
        <div ng-repeat="d in data">
            <div ng-if="!d.group">
                {{d.name}}
            </div>
        </div>
    </div>