【发布时间】:2014-03-14 20:18:58
【问题描述】:
我正在使用 following template 配置 AngularJS/Typescript Web 应用程序并收到以下错误。
The following error is appearing when I run the application:
0x800a139e - JavaScript runtime error: [$injector:modulerr] Failed to instantiate module app due to:
Error: [$injector:nomod] Module 'app' 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.
我检查了this thread 并确保我确实引用了 angular-route.js
app.ts - Easier version to view + index.html
/// <reference path="./typings/angularjs/angular.d.ts" />
'use strict';
// Create and register modules
var modules = ['app.controllers','app.directives', 'app.filters', 'app.services'];
modules.forEach((module) => angular.module(module, []));
angular.module('app', modules);
// Url routing
angular.module('app').config(['$routeProvider',
function routes($routeProvider: ng.IRouteProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/MyView.html',
controller: 'app.controllers.MyController'
})
.otherwise({
redirectTo: '/'
});
}
]);
module app {
export module controllers {}
export module directives {}
export module filters {}
export module services {}
export interface IController {}
export interface IDirective {
restrict: string;
link($scope: ng.IScope, element: JQuery, attrs: ng.IAttributes): any;
}
export interface IFilter {
filter (input: any, ...args: any[]): any;
}
export interface IService {}
/**
* Register new controller.
*
* @param className
* @param services
*/
export function registerController (className: string, services = []) {
var controller = 'app.controllers.' + className;
services.push(app.controllers[className]);
angular.module('app.controllers').controller(controller, services);
}
/**
* Register new filter.
*
* @param className
* @param services
*/
export function registerFilter (className: string, services = []) {
var filter = className.toLowerCase();
services.push(() => (new app.filters[className]()).filter);
angular.module('app.filters').filter(filter, services);
}
/**
* Register new directive.
*
* @param className
* @param services
*/
export function registerDirective (className: string, services = []) {
var directive = className[0].toLowerCase() + className.slice(1);
services.push(() => new app.directives[className]());
angular.module('app.directives').directive(directive, services);
}
/**
* Register new service.
*
* @param className
* @param services
*/
export function registerService (className: string, services = []) {
var service = className[0].toLowerCase() + className.slice(1);
services.push(() => new app.services[className]());
angular.module('app.services').factory(service, services);
}
}
我是一个 TS/Angular 新手,因此我们将不胜感激。
谢谢
【问题讨论】:
-
您使用的是哪个版本的 Typescript?该模板很旧,并且在
0.9.1.1和0.9.5分支上都抛出了错误。 -
我相信我在 0.9.21
标签: angularjs typescript