angular.forEach()

angular.forEach()

forEach迭代对象或者数组。

example:

var values = {name: 'misko', gender: 'male'};
var log = [];
var tmpVal = angular.forEach(values, function(value, key, obj) { 
  this.push(key + ': ' + value);
}, log);
console.log(values);
console.log(log);
console.log(tmpVal);

result:

Object {name: "misko", gender: "male"}//values
["name: misko", "gender: male"]//log
Object {name: "misko", gender: "male"}//tmpVal

 

angularjs的for循环中,如果访问后台接口,需要对返回值判断,这时候就会出现异步加载的问题。

用angular.forEach() 解决这个问题,代码如下(value为 $scope.uploadDataList 中的值,index 是数组的索引值):

angular.forEach($scope.uploadDataList, function(value,index){
    $scope.uploadData = value;
    $scope.uploadData.name = $scope.IPSUser.name;
    $scope.uploadData.part = inds;
    $http.post($scope.URL+"zhuanlistatus/addZhuanliStatus",$scope.uploadData).success(function(data) {
        if(data.result){
            if(index == $scope.uploadDataList.length-1) {
                layer.alert("通知书添加成功",{time:2000});
                ngDialog.closeAll();
            }
        }else{
            $scope.falseList.push(value);
            ngDialog.closeAll();
            if(index == $scope.uploadDataList.length-1) {
                ngDialog.open({
                    template: 'html/maintain/falseListPage.html',
                    className: 'ngdialog-theme-plain',
                    width: '40%',
                    scope: $scope,
                    cache: false
                });
            }
        }
    });
},$scope.falseList);

 

posted @ 2018-06-07 16:32  miny_simp  阅读(291)  评论(0编辑  收藏  举报