注入错误:无法实例化模块服务App

时间:2017-06-19 14:54:35

标签: javascript angularjs

我有在angularJS中注入模块的问题:

  

未捕获错误:[$ injector:modulerr]无法实例化模块   Mainapp由于:错误:[$ injector:modulerr]无法实例化   模块servicesApp由于:错误:[$ injector:nomod]模块   'servicesApp'不可用!您要么错误拼写了模块名称   或者忘了加载它。如果注册模块,请确保指定   依赖关系作为第二个参数。

以下是服务代码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']);

EDIT1:

我改变了'指令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不是函数   directivesApp.js:13

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

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

谢谢,

1 个答案:

答案 0 :(得分:1)

sytanx应 directive , 变化

<强> From

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

<强> To

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