如何摆脱量angular器的循环?

这是我的代码 –

formElements[0].findElements(by.repeater(repeater)).then(function(items){ console.log(i, '>>>>>>>>>.No of items in the list --- '+items.length); (function(items){ for(var x1=0; x1<itemsToBeSelected.length; x1++){ console.log(i, '>>>>>>.Looking for item --- '+itemsToBeSelected[x1]); skip = false; (function(items, x1){ for(var x2=0; x2<items.length; x2++){ (function(items, x2){ items[x2].getText().then(function(itemName){ console.log(i, '>>>>..Verifying '+itemsToBeSelected[x1]+' with '+itemName); if(itemName == itemsToBeSelected[x1]){ console.log(i, '>>>>>.Selecting the item --- '+itemName); items[x2].findElement(by.css('.regular-checkbox')).click(); } }); }(items, x2)); } }(items, x1)); } }(items)); }); 

当条件itemName == itemsToBeSelected [x1]满足时,我想打破内部for循环。 尝试使用标志,返回语句,但无法摆脱循环。

请在代码中提示更正。

  ptor.element.all(by.repeater(repeater)).then(function(products){ console.log(i, '>>>>>>>>>.Products length --- '+products.length); async.each(products, verifyName, function(err){ console.log('>>>>>>>>>>>err value --- '+err); expect(err).toBe(true); if(err){ console.log(i, '>>>>>>>>.Element is present'); }else{ console.log(i, '>>>>>>>>.Element is not present'); } }); function verifyName(product, callback){ console.log(i, '>>>>>>>>.Inside function verifyName'); product.getText().then(function(name){ console.log('>>>>>>>>>>Looking for product --- '+name); if(name==entityName){ console.log(i, '>>>>>>>>Verified the name - '+name); callback(true); } }); } }); 

我们也可以在async.each模块的帮助下获得相同的结果。 例如,我已经发布了一个代码,我试图find一个单一的值。

所以关于我的问题,我们可以在设置callback(true)之前点击或者对元素执行任何操作。 比如说我们可以这样做 – product.click();

不要在编辑之前说的话,你可以使用caolan的asynchronous模块来迭代你的数组,使用detect或detectSeries函数。

它应该看起来像这样:

 formElements[0].findElements(by.repeater(repeater)).then(function(items) { console.log(i, '>>>>>>>>>.No of items in the list --- ' + items.length); itemsToBeSelected.forEach(function(itemToBeSelected) { async.detect(items, function(item, next) { item.getText().then(function(itemName) { // This function will be called on each item in items, until next(true) is called console.log('Verifying ' + itemToBeSelected + ' with ' + itemName); // Here you call the callback with the truth value : return next(itemName === itemToBeSelected); }); }, function(item) { // This function is called with the first item that resulted in a true // callback value. console.log('Selecting the item --- ' + item); item.findElement(by.css('.regular-checkbox')).click(); }); }); });