检查Angular控制器中是否设置了var

时间:2014-04-11 16:30:15

标签: javascript angularjs

我在角度控制器中有这个功能:

$scope.sendCompanyData = function() {
    delete $scope.company["step1Form"];
    delete $scope.company["step2Form"];
    delete $scope.standard_address["state"];

    $http.post(Routing.generate('create-company'), {
        'company': $scope.company,
        'standard_address': $scope.standard_address,
        'phone': $scope.phone,
        'courrier_address': $scope.courrier_address,
        'logoFileName': $scope.logoFileName,
        'mercantilDocFileName': $scope.mercantilFileName,
        'rifDocFileName': $scope.rifFileName,
        'standardAddressState': $scope.state.standard_address,
        'standardAddressCity': $scope.city.standard_address,
        'courrierState': $scope.courrierState.courrier_address,
        'courrierCity': $scope.courrierCity.courrier_address
    }).success(function(data) {
        if (!data.success) {
            if (!data.exception) {
                $scope.errors = data.errors;
            } else {
                $scope.errors = data.exception;
            }
        } else {
            $templateCache.removeAll();
            ClientUser.loginToCompany(data.companyId);

            if ($scope.mercantilFileName != "" && $scope.rifFileName != "") {
                $noty.success(Translator.trans('company.register_success'));
            } else {
                $noty.success(Translator.trans('company.register_document_missing'));
            }

            $location.path('/empresa/' + data.companyAlias);
        }
    }).error(function(data, status) {
        $scope.error = status;
    });

    $scope = angular.element($(".seller-layout.new")).scope();
    $scope.section = 'segundo-paso';
}

与此代码相关的问题位于此行$scope.mercantilFileName != "" && $scope.rifFileName != "",因为即使我没有发送任何值,也不会检查或永远不会发生此问题。我对此感到疯狂,也许这很简单,但代码总是低于else句。对此有何帮助?

1 个答案:

答案 0 :(得分:1)

为什么不简单:

if ($scope.mercantilFileName && $scope.rifFileName) {...}

我认为$ scope.mercantilFileNam比空字符串(“”)更“未定义”。