![](/img/trans.png)
[英]angular.js:13424 ReferenceError: index is not defined when using factory method in controller
[英]angular.js: ReferenceError: up is not defined
我正在為我的項目進行上傳功能,我的按鈕包括上傳和提交功能。 單擊上傳后,在開發人員控制台中出現錯誤:
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
。
編輯 :您正在在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() });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.