我使用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
为空时,我收到此错误。我将条件设为if statement(e.g-if($scope.classData==null))
,但无法检查任何内容。请帮我解决此错误。
答案 0 :(得分:0)
检查$ scope.classData是否存在的正确方法是
if ('undefined' !== typeof $scope.classData) {}
或
if (angular.isDefined($scope.classData)) {}
我不确定你的代码的其余部分是什么样的,但是在你的控制器中你应该将$ scope.classData定义为一个空数组,这样你就可以将新项目推送或取消移动。