角度.js:引用错误:未定义向上

angular.js: ReferenceError: up is not defined

本文关键字:未定义 引用 js 角度 错误      更新时间:2023-09-26

我正在为我的项目开发上传功能,我的按钮包括上传和提交功能。单击上传后,我在开发人员控制台中收到错误:

角度.js:13236 参考错误:未定义向上

对此错误的引用说此行中的问题

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