javascript - 喷油器错误 : Failed to instantiate module servicesApp

标签 javascript angularjs

我在 angularJS 中注入(inject)模块时遇到问题:

Uncaught Error: [$injector:modulerr] Failed to instantiate module Mainapp due to: Error: [$injector:modulerr] Failed to instantiate module servicesApp due to: Error: [$injector:nomod] Module 'servicesApp' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

这是服务代码servicesApp.js:

'use strict';

var servicesApp = angular.module('servicesApp', []);

servicesApp.service('fileUpload', ['$http',function ($http) {

    this.uploadFileToUrl = function(file, uploadUrl){
        var data = new FormData();
        data.append('file', file);

        return $http.post(uploadUrl, data, {
            transformRequest: angular.identity,
             headers: { 'Content-Type': undefined }
          }).then(function (results) {
              console.log("done post "+results);
              return results;
        }).catch(function(e) {
            console.log('failed to post: ', e);
            throw e;
        });
    };

     this.upFile=function(file){

        var nameImg="";
        var uploadUrl =urlAPI+"/files/uploadFile";

        if( file != null){

            nameImg =file.name;

              this.uploadFileToUrl(file, uploadUrl);

          }else{
              nameImg="userDefault.png";
          }

      return nameImg;
   };

}]);

这是指令代码directivesApp.js:

'use strict';

var directivesApp = angular.module('directivesApp', []);

directivesApp.directives('fileModel', ['$parse', function ($parse) {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
           var model = $parse(attrs.fileModel);
           var modelSetter = model.assign;

           element.bind('change', function(){
              scope.$apply(function(){
                 modelSetter(scope, element[0].files[0]);
              });
           });
        }
     };
  }]);

这是主要代码Mainapp.js:

var app = angular.module('Mainapp', ['ngRoute','file-model','ui.bootstrap','ngMessages','angular-loading-bar','ngFileUpload','servicesApp',
    'directivesApp']);
<小时/>

编辑1:

我更改了“directives‌​App”和“servicesApp”的位置,但仍然遇到同样的问题:

var app = angular.module('Mainapp', ['ngRoute','file-model','ui.bootstrap','ngMessages','angular‌​-loading-bar','ngFil‌​eUpload','directives‌​App','servicesApp'])‌​;
<小时/>

编辑2:

Uncaught TypeError: directivesApp.directives is not a function at directivesApp.js:13

directivesApp.directives('fileModel', ['$parse', function ($parse) { ..}

如何解决这个问题? 如何在 Controller 中使用指令和服务?

谢谢,

最佳答案

语法应为指令, 改变

来自

directivesApp.directives('fileModel', ['$parse', function ($parse) { ..}

directivesApp.directive('fileModel', ['$parse', function ($parse) { ..}

关于javascript - 喷油器错误 : Failed to instantiate module servicesApp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44633809/

相关文章:

javascript - 来自 Firebase 的 Angular 显示列表

angularjs - 如何在 AngularJS 中点击不同的链接时显示/隐藏不同的数据?

javascript - Bootstrap 3 和 Angular JS - 带有表格和 div 的行

javascript - 如何在不重新编译的情况下使用 ASP.NET 捆绑和缩小?

javascript - angularjs 中相同文本字段的电子邮件或电话号码验证

javascript - 主干 View 顶级元素问题

javascript - 使用 window.location 的基于计时器的重定向问题

javascript - 在 Angular 表达式中使用对象原型(prototype)

javascript - 关闭 Chromium 信息亭模式

javascript - 如何将不同大小的圆圈对齐到中间?