【发布时间】:2017-06-19 14:54:35
【问题描述】:
我在 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']);
编辑1:
我改变了 'directivesApp' 和 'servicesApp' 的位置,但我仍然遇到同样的问题:
var app = angular.module('Mainapp', ['ngRoute','file-model','ui.bootstrap','ngMessages','angular-loading-bar','ngFileUpload','directivesApp','servicesApp']);
编辑 2:
未捕获的类型错误:directivesApp.directives 不是函数 指令App.js:13
directivesApp.directives('fileModel', ['$parse', function ($parse) { ..}
如何解决这个问题? 如何在控制器中使用指令和服务?
谢谢,
【问题讨论】:
-
在 index.html 中显示顺序
-
怎么样? @Sajeetharan
-
您为这些 js 文件添加引用的文件
-
附上问题
-
您的 html 中是否有对
serviceApp的引用?<script src="servicesApp.js"></script>
标签: javascript angularjs