【发布时间】:2016-11-30 21:47:18
【问题描述】:
我有一个 angular 应用程序,如下所示,我有一个 angular.forEach 函数,我想用 continue 关键字跳过一些值,但我不能。
<div ng-app="app">
<div ng-controller="test">
test
{{printPairs()}}
</div>
</div>
angular.module("app", [])
.controller("test", function($scope){
var array = [1,2,3,4,5,6];
$scope.printPairs = function(){
angular.forEach(array, function (elem) {
if(elem % 2 === 1){
continue;
}
//More logic below...
})
};
});
有人知道为什么会这样吗?
【问题讨论】:
-
'continue' 在 angular.forEach() 中不起作用
-
continue 语句终止当前或标记循环的当前迭代中语句的执行,并在下一次迭代中继续执行循环。 那是指本机循环,而不是由辅助函数提供的那些
-
因为 continue 可以在传统的 for 循环体内使用,但不能在传递给数组的 forEach 函数的函数中使用。只需使用
if (elem % 2 === 0) { // logic }。无论如何,这会更干净,即使在 for 循环中也是如此。
标签: angularjs