简体   繁体   English

AngularJS ng-repeat指令

[英]AngularJS ng-repeat directive

Hi I'm relatively new to AngularJS and I am creating an accordion menu. 嗨,我是AngularJS的新手,我正在创建一个手风琴菜单。 I used a template provided by the angular UI team and wanted to make a tweak to it. 我使用了角度UI团队提供的模板,并希望对其进行调整。 In one of my $scope attributes I have a variable called groups which is an array of items containing a title element and a content element, which is an array of strings. 在我的$ scope属性之一中,我有一个名为groups的变量,它是包含title元素和content元素(由字符串组成的数组)的项的数组。 I use ng-repeat to cycle through the titles and content of each, however I implement a second ng-repeat directive because I want the strings in the content array to be in the form of the list. 我使用ng-repeat循环显示每个标题和内容,但是我实现了第二个ng-repeat指令,因为我希望content数组中的字符串采用列表形式。 This second ng-repeat directive is giving me issues, because when I test it with only one set of title/content I get the expected result, but when there are multiple items (essentially more tabs on the accordion menu) it doesn't work. 第二个ng-repeat指令给我带来了问题,因为当我仅用一组标题/内容进行测试时,我会得到预期的结果,但是当有多个项目(在手风琴菜单上实际上有更多选项卡)时,它不起作用。 This is most likely a minor error in my code. 这很可能是我的代码中的一个小错误。 This is my module code in JS: 这是我在JS中的模块代码:

$scope.groups = [
{
  title: 'Title 1',
  content: ['a','b','c','d'],
}
{
  title: 'Title 2',
  content: ['a','b','c','d']
},
{
  title: 'Title 3',
  content: ['a','b','c','d']
},
{
  title: 'Title 4'
  content: ['a','b','c','d']
}

]; ];

And this is my html: 这是我的html:

<accordion-group heading="{{group.title}}" ng-repeat="group in groups">
    <ul>
        <li ng-repeat="item in group.content">{{ item }}</li>
    </ul>
</accordion-group>

This is just a small part of the code that has the bug. 这只是具有该错误的代码的一小部分。 Again, it works if, for example, only the first item on the groups array is in the code, but once I add more items to the groups array, it crashes. 再次,它可以工作,例如,如果在代码中只有groups数组中的第一项,但是一旦我将更多项目添加到groups数组中,它就会崩溃。 Thank you ahead of time for any help. 提前谢谢您的帮助。

Most likely due to duplicate item, use track by 最有可能是由于项目重复,请使用跟踪依据

<accordion-group heading="{{group.title}}" ng-repeat="group in groups">
    <ul>
        <li ng-repeat="item in group.content track by $index">{{ item }}</li>
    </ul>
</accordion-group>

I have tested that and you probably forgot the: accordion tag. 我已经测试过了,您可能忘记了: accordion标签。 The working accordion code looks like: 工作的手风琴代码如下:

<accordion close-others="oneAtATime">
    <accordion-group heading="{{group.title}}" ng-repeat="group in groups">
    <ul>
        <li ng-repeat="item in group.content">{{ item }}</li>
    </ul>
</accordion-group>
  </accordion>

Here is a link to working plunker 这是正在工作的矮人的链接

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

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