验证抛出undefined的null值

时间:2017-07-10 06:32:38

标签: javascript angularjs

我有这样的选择:

<select class="form-control" ng-hide="Catalogos.length==0"
        ng-change="filtro(selected)" ng-model="selected"
        ng-options="item.Nombre for item in Catalogos ">
</select>

从那里我得到selected value这样的函数:

$scope.filtro = function(selected) {
            $scope.selectedID = selected.ID;
        }

Id获得selected value,但现在我将它用于另一个函数,但是该函数可以获取参数null或者不是,因此函数是:

$scope.insertar = function () {
            if ($scope.catalogoid != null) {
                    var url = "../../api/Catalogo/UpdateCatalogoRegistro/" + $scope.Codigo + "/" + $scope.Nombre + "/" + $scope.catalogoid + "/" + $scope.Catalogo;

                    if ($scope.selected.ID != null) {
                        url = url + "/" + $scope.selected.ID;
                    }
                apiService.post(url....

问题是当它变为空时它只是在通过此验证时抛出$scope.selected is undefined

                 if ($scope.selected.ID != null) {
                            url = url + "/" + $scope.selected.ID;
                        }

如果在这种情况下不存在$scope.selected,我该如何验证呢?

1 个答案:

答案 0 :(得分:1)

您已将其存储在$scope.selectedID中,但是从$scope.selected.ID检索。

使用

if ($scope.selectedID != null) {