简体   繁体   English

如何使用ng-repeat angularjs模仿for循环?

[英]How can I immitate a for loop with ng-repeat angularjs?

I'm trying to display a list inside a list. 我正在尝试在列表中显示一个列表。 This loop works in JavaScript. 此循环在JavaScript中有效。

for ( i=0; self.InsuredCommoditiesList.length > i; i++){
       self.CommoditiesCategories = self.InsuredCommoditiesList[i].Category;
           for (j = 0; self.InsuredCommoditiesList[i].Items.length > j; j++) {
                        self.CommoditiesList = self.InsuredCommoditiesList[i].Items[j]; 
                    }

This is the body of my ng-repeat 这是我的ng-repeat的主体

<label ng-repeat="commo in QQCtrl.InsuredCommoditiesList track by $index">
        {{commo.Category}}
       <input type="checkbox"> {{commo.Items}}
</label>

And my result is almost correct, the problem is that "Items" are not being display individually. 我的结果几乎是正确的,问题是“项目”没有单独显示。 Instead it's just showing the whole array. 相反,它只是显示整个数组。

Example in the following picture: 下图中的示例: 在此处输入图片说明

Can I use something similar to position "j" in my ng-repeat to display the items individually? 我可以在ng-repeat中使用类似于位置“ j”的东西来单独显示项目吗?

You need to replace the inner loop of your equivalent javascript code in angularjs as well, ie you will need one more ng-repeat . 您还需要在angularjs中替换等效的javascript代码的内部循环,即,您将需要一个以上ng-repeat

Something like: 就像是:

<label ng-repeat="commo in QQCtrl.InsuredCommoditiesList track by $index">
        {{commo.Category}}
       <input type="checkbox" ng-repeat="item in commo.Items"> {{item}}
</label>

You have two loops in your JavaScript code, so you will need two loops in the angular to go through the inner list. 您的JavaScript代码中有两个循环,因此需要两个角度来遍历内部列表。

    <label ng-repeat="commo in QQCtrl.InsuredCommoditiesList track by $index">
            {{commo.Category}}
           <input type="checkbox"> 
           <label ng-repeat="item in commo.Items" >{{item}}</label>
    </label>

Untested, but should work assuming commo.Items is a list of strings 未经测试,但假定commo.Items是字符串列表,则可以使用

The items in each list can be displayed using a list, for example: an unordered list (ie <ul> ) or an ordered list (ie <ol> ), with a list item (ie <li> ) for each item in the array. 可以使用列表显示每个列表中的项目,例如: 无序列表 (即<ul> )或有序列表 (即<ol> ),以及列表中每个项目的列表项 (即<li> )。阵列。 In the example below, item is analogous to self.InsuredCommoditiesList[i].Items[j] in the for loop of the regular JavaScript example. 在下面的示例中, item类似于常规JavaScript示例的for循环中的self.InsuredCommoditiesList[i].Items[j]

<ul>
    <li ng-repeat="item in commo.Items">{{item}}</li>
</ul>

In fact, there is a repeat_expression 1 where j could be used in a similar manner: (key, value) in expression , which would look like below: 实际上,有一个repeat_expression 1 ,其中j可以类似的方式使用: (key, value) in expression如下所示:

<li ng-repeat="(j,item) in commo.Items">{{item}}</li>

A <label> is only permitted to only contain Phrasing content 2 but the lists are Flow Content so move the ngRepeat up to another parent element like a <div> or a <span> . <label>只允许包含短语内容 2,但是列表是Flow Content,因此将ngRepeat移至另一个父元素,例如<div><span> Then make the label, input and list tags child elements. 然后使标签,输入和列表标签成为子元素。

<div ng-repeat="commo in QQCtrl.InsuredCommoditiesList track by $index">
    <label for="checkbox_{{$index}}">{{commo.Category}}</label>
    <input type="checkbox" id="checkbox_{{$index}}">
    <ul>
        <li ng-repeat="item in commo.Items">{{item}}</li>
    </ul>
</div>

See a demonstration of this below. 请参阅下面的演示。

 angular.module('QQapp', []) .controller('ctrl', function($scope) { this.InsuredCommoditiesList = [{ "id": 3, "Category": "Agricultural liquids - Petroleum", "Items": ["100% Produce", "Alcohol", "Appliances"] }, { "id": 4, "Category": "Grocery Items (dry)", "Items": ["Candy", "Canned goods", "Containers"] }, { "id": 6, "Category": "Building materials", "Items": ["Alfalfa", "All Non-perishable General Merchandise", "Almonds"] } ]; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="QQapp" ng-controller="ctrl as QQCtrl"> <span ng-repeat="commo in QQCtrl.InsuredCommoditiesList track by $index"> <label for="checkbox_{{$index}}">{{commo.Category}}</label> <input type="checkbox" id="checkbox_{{$index}}"> <ul> <li ng-repeat="(j,item) in commo.Items" id="{{commo.id}}_{{j}}" >{{item}}</li> </ul> </span> </div> 


1 refer to the Arguments section of ngRepeat for supported formats 1请参阅ngRepeatArguments部分以获取受支持的格式

2 https://developer.mozilla.org/en-US/docs/Web/HTML/Element/label 2 https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/label

You may try for this: 您可以为此尝试:

<label ng-repeat="commo in QQCtrl.InsuredCommoditiesList track by $index">
        {{commo[0].Category}}
       <input type="checkbox"> {{commo[0].Items}}
</label>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM