每当我尝试使用require.js注入依赖项时,我都会收到角度模块错误
我的bower.json文件:
{
"name": "asa",
"version": "1.0",
"authors": [
"asda"
],
"description": "aaaa",
"dependencies": {
"angular": "1.3.8",
"angular-route": "1.3.8",
"requirejs": "2.1.15",
},
"license": "sss",
"homepage": "index.html",
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"src/main/webapp/resources/bower_components",
"test",
"tests"
]
}
的 Main.js
var app = angular.module('myApp', ['route']);
app.service('abc', function () {
//some content
});
app.service('def', function( $http, $q ) {
//some content
});
var zzz = app.controller('zzz', function ($scope, $http, $filter, abc, def) {
//controller content
});
zzz.$inject = ['$scope', '$filter','abc','def'];
我的数据主文件包含依赖项:main-di.js
require.config({
paths: {
angular: '../lib/dependencies/bower_components/angular/angular',
route: '../lib/dependencies/bower_components/angular-route/angular-route',
myApp: "person"
},
shim: {
angular: {
exports: "angular"
},
route: {
deps: ['angular']
},
myApp: {
deps: [ 'angular', 'route']
}
}
});
require(['myApp'], function () {
angular.bootstrap(document.getElementById('myApp'), ['myApp']);
});
在运行应用程序时,我收到以下错误 -
错误:
TypeError: e is undefined
...a("$ngControllerController",f));w(a)}}}n=e.module("ngRoute", ["ng"]).provider("$r...
Error: [$injector:modulerr] Failed to instantiate module myApp due to:
[$injector:nomod] Module 'myApp' 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.
http://errors.angularjs.org/1.3.8/$injector/nomod?p0=myApp minErr/<@http://localhost:8080/AngularJsInJava/lib/dependencies/bower_components/angular/angular.js:63:12
有人可以帮我解释为什么我会收到此错误吗?
答案 0 :(得分:1)
这不是定义控制器的正确方法。
var zzz = app.controller('zzz', function ($scope, $http, $filter, abc, def) {
//controller content
});
以这种方式创建控制器..
app.controller('zzz', ZzzCtrl);
ZzzCtrl.$inject = ['$scope', '$filter','abc','def'];
function ZzzCtrl ($scope, $filter, abc, def) {
// controller code here
}
此外,如果您使用minification作为角度代码,则应始终使用注释或“$ inject”。不仅限于控制器。 例如。
angular.module('someModule').service('someService',['$someInjection', function($someInjection){}])