无法在Angular Controller中读取未定义的属性'indexOf'

时间:2016-06-08 08:39:22

标签: javascript angularjs

我遇到了“无法读取未定义的属性'indexOf'的问题”。以下是源代码。感谢您的帮助。

$http.get("js/project/data.json").then(function(response) {
        //$scope.case = response.data.caseno;    
        $scope.case = response.data.caseno.map(function(elem) { 
            return elem.toLowerCase(); 
        }); 
    });

    console.log($scope.project.ProjectCaseNo.toLowerCase());

    if($scope.case.indexOf($scope.project.ProjectCaseNo.toLowerCase()) != -1 ){
        $scope.glyphicon = 'glyphicon-ok green';
        $scope.CaseMessage = 'Valid Case No.';
    }else{
        $scope.glyphicon = 'glyphicon-remove red';
        $scope.CaseMessage = 'Invalid Case No.';
    };
};

2 个答案:

答案 0 :(得分:2)

改变它是这样的:

    $scope.VerifyCaseNo = function(){
        $http.get("js/project/data.json").then(function(response) {
            //$scope.case = response.data.caseno;    
            $scope.case = response.data.caseno.map(function(elem) { 
                return elem.toLowerCase(); 
            }); 
            console.log($scope.project.ProjectCaseNo.toLowerCase());

            if($scope.case.indexOf($scope.project.ProjectCaseNo.toLowerCase()) != -1 ){
                $scope.glyphicon = 'glyphicon-ok green';
                $scope.CaseMessage = 'Valid Case No.';
            }else{
                $scope.glyphicon = 'glyphicon-remove red';
                $scope.CaseMessage = 'Invalid Case No.';
            };
        });
    };

$scope.case操作完成之前,您的$http.get()将被取消定义。所以你得到了未定义错误的无法读取属性'indexOf'!

答案 1 :(得分:0)

您可以使用String(yourValue).indexOf('')