簡體   English   中英

使用Angular.js和PHP獲取TypeError

[英]Getting TypeError using Angular.js and PHP

我在使用angular.js和PHP時遇到以下錯誤。

錯誤:

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)

我在下面解釋我的代碼。

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 });
                    }
                });
            }
        }

我在$scope.classData為null時收到此錯誤。我放入了if statement(eg-if($scope.classData==null))的條件if statement(eg-if($scope.classData==null))但它無法檢查任何內容。請幫助我解決此錯誤。

檢查$ scope.classData是否存在的正確方法是

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

要么

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

我不確定代碼的其余部分是什么樣子,但在控制器內應將$ scope.classData定義為空數組,以便可以將新項目壓入或取消移位。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM