gpt4 book ai didi

javascript - Angular .js : ReferenceError: up is not defined

转载 作者:行者123 更新时间:2023-12-03 07:06:36 24 4
gpt4 key购买 nike

我正在为我的项目开发上传功能,我的按钮包括上传和提交功能。一旦我点击上传,我就会在开发者控制台中收到错误消息:

angular.js:13236 ReferenceError: up is not defined

此错误的引用表明该行存在问题

$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...
});
});
};
}]);

最佳答案

您的 Controller 中未定义

up 变量。相反,定义了 vm

因此,请使用vm.filevm.submit

编辑:您在 submit 方法中定义 watch 表达式,并从 watch 表达式调用 submit 方法。

您应该在 submit 方法之外定义 watch,如下所示:

    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: up is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36799419/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com