angular.js - angular.forEach怎么跳出循环
滿天的星座
滿天的星座 2017-05-15 16:54:21
0
4
929

如题,angular.forEach怎么做可以跳出循环?

滿天的星座
滿天的星座

reply all(4)
大家讲道理

Let me answer your question first:

forEach cannot break out of the loop.

In most programming languages, foreach不仅仅是语法糖,通过编译优化它可以提供更好的性能,比如直接略去边界检查。为了能更好地做到这些优化,foreachalso imposes design constraints. For example, in C#, the iterative process does not allow changes to the container itself (adding or deleting elements), and breaks are not allowed (we know that all conditional statements will reduce the performance of the instruction cache and pipeline).

Angular wraps a series of native JS methods to better monitor model changes. The usage of these JS methods is basically the same as before. The forEach就是一个,还有$timeout you mentioned is just one, and there are also $timeout and so on. For information about Angular’s ​​data binding method, you can see this: http://harttle.com/2015/06/06/angular-data-binding-and-digest.html

Ty80

http://stackoverflow.com/a/13844508/2586541


var keepGoing = true;
angular.forEach([0,1,2], function(count){
  if(keepGoing) {
    if(count == 1){
      keepGoing = false;
    }
  }
});
仅有的幸福

Js’s native forEach and jquery’s each are like this, return true, interrupt the subsequent operation, and continue traversing to the next operation, similar to continue; return false, end the entire traversal, similar to break
I'm not sure about Angular. You can give it a try. This kind of syntactic sugar should be similar.

世界只因有你

@harttle When the looped data is too large, it won’t affect the performance very much.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template