简体   繁体   English

$ index和量角器的AngularJS ng-repeat跟踪重复

[英]AngularJS ng-repeat track by $index and Protractor with Duplicates

I have an array of strings, which I need to allow for duplicates. 我有一个字符串数组,我需要允许重复。

vm.list = ["item","item","item","item","item"]

this is handled in the html with 这是用html处理的

<ul class="listItems>
    <li ng-repeat="item in ctrl.list track by $index"></li>
</ul>

This displays fine in the DOM, no issues, but I run into problems when I try to protractor test the ng-repeat, since I can't unit test it. 这在DOM中显示正常,没有问题,但是当我尝试量角器测试ng-repeat时遇到了问题,因为我无法对其进行单元测试。

So my test is something like. 所以我的测试有点像。

Then("List items should contain {int} items.", function(listLength){
    return element(by.css(".listItems").all(by.repeater('item in ctrl.list track by $index')).then(function(list){
        return expect(list.length).equal(listLength);
    });
});

I run my tests and it fails with Expect 0 to be 5 But if I make them all unique it works fine, how can I fix this? 我运行测试,但失败,期望0为5。但是,如果我使它们全部唯一,则可以正常工作,如何解决此问题?

I believe this should work for you: 我相信这应该为您工作:

Then("List items should contain {int} items.", function(listLength){
    let list = element(by.css(".listItems").all(by.repeater('item in ctrl.list'));
    expect(list.count()).toBe(listLength);
});

Source: http://www.protractortest.org/#/api?view=ElementArrayFinder.prototype.count 来源: http : //www.protractortest.org/#/api? view= ElementArrayFinder.prototype.count

Edit: Sorry, I misinterpreted your question, it appears track by does not work with duplicate values in an array. 编辑:对不起,我误解你的问题,看来track by不与数组中的重复值工作。 See: https://docs.angularjs.org/api/ng/directive/ngRepeat . 请参阅: https : //docs.angularjs.org/api/ng/directive/ngRepeat You can substitute your own tracking function. 您可以替换自己的跟踪功能。

Here is an example of a similar issue: https://stackoverflow.com/a/28232640/3111958 这是一个类似问题的示例: https : //stackoverflow.com/a/28232640/3111958

2nd Edit: After looking into it some more, protractor does not use the track by portion. 第2次修改:多看了几遍之后,量角器不再按比例使用轨迹。 I updated the code block to reflect that. 我更新了代码块以反映这一点。 Check out this issue: https://stackoverflow.com/a/37535098/3111958 查看此问题: https : //stackoverflow.com/a/37535098/3111958

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

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