使用Angular.js和PHP获取TypeError

时间:2022-06-11 21:36:50

I am getting the following error using angular.js and PHP.

我使用angular.js和PHP得到以下错误。

Error:

TypeError: $scope.classData.unshift is not a function
    at successCallback (adminResourceClassController.js:55)
    at angularjs.js:118
    at n.$eval (angularjs.js:132)
    at n.$digest (angularjs.js:129)
    at n.$apply (angularjs.js:133)
    at g (angularjs.js:87)
    at K (angularjs.js:91)
    at XMLHttpRequest.z.onload (angularjs.js:92)

I am explaining my code below.

我在下面解释我的代码。

if($scope.buttonName=="Add"){
            if($scope.colg_name.value=='' || $scope.colg_name.value==null){
                alert('select college name');
                classField.borderColor('colg_name');
            }else if($scope.subject==null || $scope.subject==''){
                alert('Subject Type field could not be blank...');
                classField.borderColor('resourcesub');
            }else if($scope.period=='' || $scope.period==null){
                alert('Please select the no of period...');
                classField.borderColor('resourceperiod');
            }else{
                var userdata={'colg_name':$scope.colg_name.value,'sub_type':$scope.subject,'period':$scope.period};
                $http({
                    method:'POST',
                    url:"php/resource/addClassData.php",
                    data:userdata,
                    headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
                }).then(function successCallback(response){
                    alert(response.data['msg']);
                    $scope.colg_name.value='';
                    $scope.subject=null;

                    $scope.classData.unshift(response.data);
                },function errorCallback(response) {
                    alert(response.data['msg']);
                    if($scope.subject==response.data.type){
                        classField.borderColor('resourcesub');
                    }else{
                    $state.go('dashboard.res.class',{}, { reload: true });
                    }
                });
            }
        }

I am getting this error when $scope.classData is null.I put the condition of if statement(e.g-if($scope.classData==null)) but it can not check anything.Please help me to resolve this error.

当$ scope.classData为null时,我收到此错误。我把if语句的条件(例如if-if($ scope.classData == null))但它无法检查任何内容。请帮我解决此错误。

1 个解决方案

#1


0  

The proper way to check if $scope.classData exists is with

检查$ scope.classData是否存在的正确方法是

if ('undefined' !== typeof $scope.classData) {}

or

if (angular.isDefined($scope.classData)) {}

I'm not sure what the rest of your code looks like but inside your controller you should define $scope.classData as an empty array, so you can push or unshift new items into it.

我不确定你的代码的其余部分是什么样的,但是在你的控制器中你应该将$ scope.classData定义为一个空数组,这样你就可以将新项目推送或取消移动。

#1


0  

The proper way to check if $scope.classData exists is with

检查$ scope.classData是否存在的正确方法是

if ('undefined' !== typeof $scope.classData) {}

or

if (angular.isDefined($scope.classData)) {}

I'm not sure what the rest of your code looks like but inside your controller you should define $scope.classData as an empty array, so you can push or unshift new items into it.

我不确定你的代码的其余部分是什么样的,但是在你的控制器中你应该将$ scope.classData定义为一个空数组,这样你就可以将新项目推送或取消移动。