我在 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:
我更改了“directivesApp”和“servicesApp”的位置,但仍然遇到同样的问题:
var app = angular.module('Mainapp', ['ngRoute','file-model','ui.bootstrap','ngMessages','angular-loading-bar','ngFileUpload','directivesApp','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/