Like the title, how to use angular.forEach to break out of the loop?
大家讲道理2017-05-15 16:56:21
Let me answer your question first:
forEach
cannot break out of the loop.
In most programming languages, foreach
不仅仅是语法糖,通过编译优化它可以提供更好的性能,比如直接略去边界检查。为了能更好地做到这些优化,foreach
also 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
PHP中文网2017-05-15 16:56:21
http://stackoverflow.com/a/13844508/2586541
var keepGoing = true;
angular.forEach([0,1,2], function(count){
if(keepGoing) {
if(count == 1){
keepGoing = false;
}
}
});
仅有的幸福2017-05-15 16:56:21
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.
世界只因有你2017-05-15 16:56:21
@harttle When the looped data is too large, it won’t affect the performance very much.