Anton
Anton

Reputation: 796

angular.js: ReferenceError: up is not defined

I'm working on upload function for my project, my button are including functions upload and submit. As soon as I click on upload I'm getting a error in developer console:

angular.js:13236 ReferenceError: up is not defined

reference on this error says that the problem in this line

$scope.$watch("up.file", function() { if (up.file) up.submit() });

exactly in if (up.file) but in meantime, everything works fine. Upload function works and all files are uploading. So I appreciate if someone could explain me where is my mistake?

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

Upvotes: 0

Views: 1164

Answers (1)

Ioan
Ioan

Reputation: 5187

up variable is not defined in your controller. Instead, vm is defined.

So, use vm.file and vm.submit.

Edit: You are defining your watch expression in the submit method, and you are calling the submit method from the watch expression.

You should define the watch outside of the submit method, like the following:

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

Upvotes: 1

Related Questions