AngularJS [$ injector:itkn]不正确的注入令牌!预期的服务名称为字符串,具有功能

时间:2018-10-25 14:08:37

标签: angularjs

我对使用AngularJS有点陌生,我需要向应用程序添加一个新功能,其中包括将附件上传到服务器上的文件夹。当我在控制器中添加“ requisition”参数之后列出的功能时,我在此帖子的标题中得到了错误消息,该错误消息指向了requisition函数。

如果我不得不猜测为什么会这样,我会说这是因为控制器的参数比请求函数多一个,但是我对Angular的结构还不够了解。在我读过的关于此错误消息的所有文章中,它通常来自于参数中的添加“,”,但我认为这次并非如此。

Javascript:

var controllerId = 'requisition';
angular.module('app').controller(controllerId, ['routemediator', 'common', 'datacontext', '$scope', 'bootstrap.dialog', '$routeParams', '$location', '$window', '$uibModalStack', 'Upload', requisition, function ($scope, Upload) {
    // upload later on form submit or something similar
    $scope.submit = function () {
        if ($scope.form.file.$valid && $scope.file) {
            $scope.upload($scope.file);
        }
    };

    // upload on file select or drop
    $scope.upload = function (file) {
        Upload.upload({
            url: 'upload/url',
            data: { file: file, 'username': $scope.username }
        }).then(function (resp) {
            console.log('Success ' + resp.config.data.file.name + 'uploaded. Response: ' + resp.data);
        }, function (resp) {
            console.log('Error status: ' + resp.status);
        }, function (evt) {
            var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
            console.log('progress: ' + progressPercentage + '% ' + evt.config.data.file.name);
        });
    };
}]);

发生错误的位置:

function requisition(routemediator, common, datacontext, $scope, bsDialog, $routeParams, $location, $window, $uibModalStack, Upload) {
*lots of code....*
}

1 个答案:

答案 0 :(得分:0)

从控制器初始化中删除function ($scope, Upload)

var controllerId = 'requisition';
    angular.module('app').controller(controllerId, ['routemediator', 'common', 'datacontext', '$scope', 'bootstrap.dialog', '$routeParams', '$location', '$window', '$uibModalStack', 'Upload', requisition]);

像下面这样在function ($scope, Upload)中添加function requisition的代码

function requisition(routemediator, common, datacontext, $scope, bsDialog, $routeParams, $location, $window, $uibModalStack, Upload) {
 // upload later on form submit or something similar
        $scope.submit = function () {
            if ($scope.form.file.$valid && $scope.file) {
                $scope.upload($scope.file);
            }
        };

        // upload on file select or drop
        $scope.upload = function (file) {
            Upload.upload({
                url: 'upload/url',
                data: { file: file, 'username': $scope.username }
            }).then(function (resp) {
                console.log('Success ' + resp.config.data.file.name + 'uploaded. Response: ' + resp.data);
            }, function (resp) {
                console.log('Error status: ' + resp.status);
            }, function (evt) {
                var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
                console.log('progress: ' + progressPercentage + '% ' + evt.config.data.file.name);
            });
        };
*lots of code....*
}