Angular JS : Cannot read property 'substring' of undefined

$http({
            method : 'GET',
            url : urls,
            // 数据类型
            dataType : "json",
            // 要传递的数据
            data : "",
        }).then(function successCallback(response) {
            $scope.credit = response.data;
            var ceditScope=$scope.credit.credit_score;
            $scope.updateTime="更新于:"+response.data.updateTime.substring(0,10);         

            $scope.change(ceditScope);
            $('.thirdline').text($scope.updateTime);
        }, function errorCallback(response) {

        })

报此错误原因在于对字符串做substring时,没有判断其是否为空。

按如下方式修改后即可解决:

$http({
            method : 'GET',
            url : urls,
            // 数据类型
            dataType : "json",
            // 要传递的数据
            data : "",
        }).then(function successCallback(response) {
            $scope.credit = response.data;
            var ceditScope=$scope.credit.credit_score;
            if(!!response.data.updateTime){
                $scope.updateTime="更新于:"+response.data.updateTime.substring(0,10);         
                $('.thirdline').text($scope.updateTime);
            }else{
                $('.thirdline').text("");
            }
            $scope.change(ceditScope);
        }, function errorCallback(response) {

        })

猜你喜欢

转载自blog.csdn.net/ly690226302/article/details/80689707