如何在角度js中同步执行代码

时间:2015-06-14 05:29:05

标签: jquery angularjs asynchronous

我面临一个问题,因为我从服务获取数据并绑定到UI,其中有一些文本框和下拉列表,我正在传递模型,因为它正确地绑定了UI。

问题,下拉其异步工作(我的猜测)。请查看以下代码

vendorService.getVendorDetailsForVendor().then(function(vendorDetails) {
            if (vendorDetails.Id !== 0) {

                $scope.businessType = vendorDetails.BusinessTypeId;
                $scope.vendorType = vendorDetails.VendorTypeId;
                $scope.category = vendorDetails.ShopCategory;
                $scope.discountUnit = vendorDetails.DiscountUnitId;
                $scope.SelectedState = vendorDetails.StateId;
                $scope.SelectedCityId = vendorDetails.CityId;


                if ($scope.businessType != null) {
                    $scope.$apply(function() {
                        for (var i = 0; i < $scope.businessTypeList.data.length; i++) {
                            if ($scope.businessTypeList.data[i].BusinessTypeId === $scope.businessType) {
                                $scope.businessType = i + 1;
                            }
                        }
                    });
                }

                if ($scope.vendorType != null) {
                    $scope.$apply(function() {
                        for (var i = 0; i < $scope.vendorTypeList.data.length; i++) {
                            if ($scope.vendorTypeList.data[i].VendorTypeId === $scope.vendorType) {
                                $scope.vendorType = i + 1;

                            }
                        }
                    });
                }

                if ($scope.category != null) {
                    console.log($scope.category);
                    $scope.$apply(function() {
                        for (var i = 0; i < $scope.categoryList.data.length; i++) {
                            if ($scope.categoryList.data[i].ShopName === $scope.category) {
                                $scope.category = i + 1;
                            }
                        }
                    });
                }


                if ($scope.discountUnit != null) {
                    $scope.$apply(function() {
                        for (var i = 0; i < $scope.discountUnitList.data.length; i++) {
                            if ($scope.discountUnitList.data[i].DiscountUnitId === $scope.discountUnit) {
                                $scope.discountUnit = i + 1;

                            }
                        }
                    });
                }

                if ($scope.SelectedState != null) {
                    $scope.$apply(function() {
                        for (var i = 0; i < $scope.stateList.data.length; i++) {
                            if ($scope.stateList.data[i].Id === $scope.SelectedState) {
                                $scope.SelectedState = i + 1;

                            }
                        }
                    });
                }

                if ($scope.SelectedCityId != null && $scope.SelectedState != undefined) {
                    $scope.$apply(function() {
                        vendorService.getCity($scope.SelectedState).then(function(cityList) {
                            $scope.cityList = {};
                            $scope.cityList.data = cityList;
                            for (var i = 0; i < $scope.cityList.data.length; i++) {
                                if ($scope.cityList.data[i].Id === $scope.SelectedCityId) {
                                    $scope.SelectedCity = $scope.SelectedCityId;
                                }
                            }
                        }, function() {
                            alert("error while fetching from server");
                        });
                    });
                }
                $scope.$apply(function() {
                    $scope.inputData = vendorDetails;
                });
                     $scope.loading = false;
            }
        }, function() {
            alert("error while fetching from server");
        });

我在Chrome控制台中收到错误

错误:无法读取属性&#39;数据&#39;未定义的

2 个答案:

答案 0 :(得分:1)

使用定义类型的angular js处理空检查。之所以可以这样做是因为与JavaScript中===的==区别,即将某些类型的值转换为其他类型的“相等”值以检查是否相等,而===则相反,后者仅检查这些值是否相等。因此,基本上==运算符知道将未定义的“”,null转换为假值。正是您所需要的。

答案 1 :(得分:0)

您应该检查是否有定义的内容,而不是与null不同的内容。像这样:

// returns true if it is a truly value (object, array, number bigger than 0, ...)
// returns false if it is a falsy value (0, '', undefined, null)

if ($scope.category) {
    // ... code
}

这是一个很酷的link that explains this