我正在为我的项目进行上传功能,我的按钮包括上传和提交功能。单击上传后,在开发人员控制台中出现错误:
angular.js:13236 ReferenceError:尚未定义
关于此错误的参考说明此行中的问题
$scope.$watch("up.file", function() { if (up.file) up.submit() });
完全在
if (up.file)
中,但与此同时,一切正常。上载功能有效,所有文件都正在上载。因此,如果有人可以向我解释我的错误在哪里,我将不胜感激。 app.controller('uploadCtrl',['$scope', '$http','Upload','$window',function($scope, $http,Upload,$window){
var vm = this;
vm.submit = function(){ //function to call on form submit
if (vm.upload_form.file.$valid && vm.file) {//check if from is valid
//console.log(vm.file.name);
vm.upload(vm.file); //call upload function
//vm.file.name = prompt("put you name");
}
$scope.$watch("up.file", function() { if (up.file) up.submit() });
};
vm.upload = function (file) {
Upload.upload({
url: '/upload', //webAPI exposed to upload the file
data:{file:file} //pass file as data, should be user ng-model
}).then(function (resp) { //upload function returns a promise
if(resp.data.error_code === 0){ //validate success
$window.alert('Success ' + resp.config.data.file.name + ' uploaded.');
} else {
$window.alert('an error occured');
}
}, function (resp) { //catch error
console.log('Error status: ' + resp.status);
$window.alert('Error status: ' + resp.status);
}, function (evt) {
console.log(evt);
var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
console.log('progress: ' + progressPercentage + '% ' + evt.config.data.file.name);
vm.progress = 'progress: ' + progressPercentage + '% '; // capture upload progress
setTimeout(10000);
}).then (function() {
$http.get('/compare').success(function() {
setTimeout(function() { alert("success!"); }, 5000);
// url was called successfully, do something
// maybe indicate in the UI that the batch file is
// executed...
});
});
};
}]);
最佳答案
up
变量未在控制器中定义。而是定义了vm
。
因此,使用vm.file
和vm.submit
。
编辑:
您在watch
方法中定义submit
表达式,并从submit
表达式调用watch
方法。
您应该在watch
方法之外定义submit
,如下所示:
vm.submit = function(){ //function to call on form submit
if (vm.upload_form.file.$valid && vm.file) {//check if from is valid
//console.log(vm.file.name);
vm.upload(vm.file); //call upload function
//vm.file.name = prompt("put you name");
}
};
$scope.$watch("vm.file", function() { if (vm.file) vm.submit() });
关于javascript - angular.js:ReferenceError:未定义,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36799419/