Home  >  Q&A  >  body text

angular.js - angular.forEach怎么跳出循环

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

滿天的星座滿天的星座2713 days ago901

reply all(4)I'll reply

  • 大家讲道理

    大家讲道理2017-05-15 16:56:21

    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

    reply
    0
  • PHP中文网

    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;
        }
      }
    });
    

    reply
    0
  • 仅有的幸福

    仅有的幸福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.

    reply
    0
  • 世界只因有你

    世界只因有你2017-05-15 16:56:21

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

    reply
    0
  • Cancelreply