protractor icon indicating copy to clipboard operation
protractor copied to clipboard

How to access nested ng-repeat elements

Open AlexCppns opened this issue 12 years ago • 5 comments

I am trying something like:

element.all(by.repeater('suggestion_type in searchBoxData.suggestion_types')).then(function(rows) {
  for(var i=0;i<rows.length;i++){
    rows[i].then(function(){
      element.all(by.repeater('content in suggestion_type.contents')).then(function(rows_2) {
        console.log(rows_2);
      });
    });
  }
});

but I am getting duplicate rows...

AlexCppns avatar Apr 17 '14 18:04 AlexCppns

Update, this is how far I got:

element.all(by.repeater('suggestion_type in searchBoxData.suggestion_types')).then(function(rows) {
  for(var i=0;i<rows.length;i++){
    rows[i].getText().then(function(foo){
       console.log(foo);
    });
    rows[i].then(function(row){
      row.findElements(by.binding('suggestion_type.name')).then(function(res){
        res[0].getText().then(function(name){
          console.log(name);
        });

      });

      row.findElements(by.repeater('content in suggestion_type.contents')).then(function(rows_2) {
        for(var j=0; j<rows_2.length;j++){
          console.log(rows_2[j]);
        }
      });
    });
  }
});

What I am trying to do is use name as a condition for the last loop. I am getting really odd ouputs though. foo gives me the text content of the first ng-repeat and the one nested in it besides for the first row where it gives me only the first ng-repeat values.

AlexCppns avatar Apr 17 '14 19:04 AlexCppns

Sorry for the delay, missed this issue:

suppose your set up is as follows:

<div ng-repeat="x in list1"><div ng-repeat="y in list2">{{x}}{{y}}</div></div>

The reason you're getting duplicates is because you're iterating all of the nested elements many times (in fact you are iterating (list1.length)^2 * (list2.length) times instead of list1.length*list2.length as you intended), because in your inner loop, you're not saying search within the outer ng-repeat, you're searching within the entire dom.

This will fix it:

element.all(by.repeater('suggestion_type in searchBoxData.suggestion_types')).then(function(rows) {
  for(var i=0;i<rows.length;i++){
    rows[i].all(by.repeater('content in suggestion_type.contents')).then(function(rows_2) {
      console.log(rows_2);
      for(var j=0;j<rows_2.length;j++){
        rows_2.getText().then(function(text) {
          console.log(text);
        });
      }
    });
  }
});

note the rows[i].all(by.repeater...) instead of element.all(by.repeater...) in your inner loop.

To make it simpler you can just do this:

element.all(by.repeater('content in suggestion_type.contents')).each(function(elem) {
  elem.getText().then(function(text) {
    console.log(text);
  });
});

Hope this answers your question

hankduan avatar Jun 23 '14 21:06 hankduan

Haven't heard anything, closing old issues.

juliemr avatar Jul 22 '14 20:07 juliemr

Hello,

Sorry I moved to other problems since. I'll have a look when I go back to E2E tests.

AlexCppns avatar Jul 22 '14 20:07 AlexCppns

Hi All,

I am testing an example using protractor. I need to select the data which is showing in ng-repeate

  • getting error. Any help much appreciated. Thanks

    element.all(by.repeater('branch in branches')).get(1).element(by.linkText('branchName')).click()

    reddynr avatar Dec 06 '16 21:12 reddynr