在ng repeat的第一行中获取第一个值

时间:2017-01-08 18:38:05

标签: javascript angularjs protractor

查看来源:

<div class="well tile single-tile">
<thread></thread>
<table class="table table-hover table-striped table-clickable">
    <tbody>
        <tr>
            <th>Make</th>
            <th>Model</th>
            <th>Color</th>
            <th>Doors</th>
            <th>MPG</th>
        </tr>
    </tbody>
    <tbody>
        <!-- ng-repeat: car in cars -->
       <tr ng-repeat="car in cars" ng-click="car result" class="ng-scope">
        <td class="ng-binding">Toyota</td><!--{{car.Make}} -->
        <td class="ng-binding">Corolla</td><!--{{car.Model}} -->
        <td class="ng-binding">White</td>
        <td class="ng-binding">4</td>
        <td class="ng-binding">21</td><!--{{car.MPG}} -->
       </tr>
       <tr ng-repeat="car in cars" ng-click="car result" class="ng-scope">
        <td class="ng-binding">Toyota</td><!--{{car.Make}} -->
        <td class="ng-binding">Camry</td><!--{{car.Model}} -->
        <td class="ng-binding">Black</td>
        <td class="ng-binding">4</td>
        <td class="ng-binding">20</td><!--{{car.MPG}}
       </tr>
    </tbody>
</table>

量角器: 我能够使用

打印第一行中的所有值
element.all(by.repeater('car in cars')).first().getText().then(function(text){and console.log(text)

在控制台中我成功地看到了“丰田卡罗拉白4 21”。

但我想只打印第一行和第二行的第一个值“丰田”。我尝试使用first().get(1).getText().then(function(text)first('["$index==1"]').getText(),但它无效。我的测试是验证第一行和第二行中是否存在“丰田”以外的值,它应该会失败..

1 个答案:

答案 0 :(得分:1)

在item.key locators

上链接并扩展Binding
element.all(by.repeater('car in cars')).first().element(by.binding('car.Make')).getText();

所有元素

element.all(by.repeater('car in cars')).each(function(element, index) {
    element(by.binding('car.Make')).getText().then(function(text) {
        console.log(text);
    });
}); //there might be a better way to do this