角模块配置不叫 [英] Angular module config not called
问题描述
我试图让我的周围AngularJS头,遇到了一个问题。结果
VAR对myApp = angular.module('对myApp',['myApp.services']);myApp.config(['$ routeProvider','$ locationProvider',函数($ routeProvider,$ locationProvider){ 的console.log('配置模块对myApp'); $ routeProvider.when('/仪表板',{templateUrl:部分/仪表盘,控制器:DashboardController});
$ routeProvider.when('/菜单',{templateUrl:部分/其他,控制器:OtherController});
$ routeProvider.otherwise({redirectTo:'/仪表板'}); $ locationProvider.html5Mode(真);
}]);
据我了解配置功能应该当模块被加载调用。 但事实并非如此!
我有 NG-应用上的
标记。和脚本加载的身体在正确的顺序的结束标记(只要该事项)。&LT =对myApp
指令;身体GT;
<脚本SRC =angular.js>< / SCRIPT>
&所述; SCRIPT SRC =app.js>&下; /脚本>
&所述; SCRIPT SRC =services.js>&下; /脚本>
&所述; SCRIPT SRC =controllers.js>&下; /脚本>
< /身体GT;
我是pretty知道我必须在这里可以俯瞰琐碎的东西。应该有打印到控制台的消息。但它仍然是空的,没有错误或警告任何责任。
不依赖于路由应用程序的运行部分就好。
更新:我service.js简化版本
使用严格的;angular.module('对myApp',函数($提供){
$ provide.factory('为myService',函数($ HTTP){
VAR的服务=功能(){
...
服务实现
...
}; 返回新服务();
});
});
看来我用来定义服务的方法重写我的对myApp
模块。
这是压倒一切的行
angular.module('对myApp',函数($提供)...
这code简单地重新定义了对myApp
模块并增加了一个功能,配置之一。
我重构service.js这个简化版本,并开始工作。
VAR对myApp = angular.module('对myApp');myApp.factory('securityService',函数(){
返回SomeFancyServiceObject();
});
I'm trying to get my head around AngularJS and ran into a problem.
var myApp = angular.module('myApp', ['myApp.services']);
myApp.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
console.log('Configuring module "myApp"');
$routeProvider.when('/dashboard', {templateUrl: 'partial/dashboard', controller: DashboardController});
$routeProvider.when('/menu', {templateUrl: 'partial/other', controller: OtherController});
$routeProvider.otherwise({redirectTo: '/dashboard'});
$locationProvider.html5Mode(true);
}]);
To my understanding the configuration function should be called when the module gets loaded. But it does not!
I have the ng-app="myApp"
directive on the <body>
tag. And scripts loaded just before the body closing tag in the correct order (as far as that matters).
<script src="angular.js"></script>
<script src="app.js"></script>
<script src="services.js"></script>
<script src="controllers.js"></script>
</body>
I'm pretty sure I must be overlooking something trivial here. There should be a message printed to the console. But it remains empty with no errors or warnings whatsoever.
The parts of the app not depending on the routes runs just fine.
Update: simplified version of my service.js
'use strict';
angular.module('myApp', function ($provide) {
$provide.factory('myService', function ($http) {
var service = function () {
...
service implementation
...
};
return new service();
});
});
It seems that the method I used for defining the service was overriding my myApp
module.
This is the overriding line
angular.module('myApp', function ($provide) ...
This code simply redefines the myApp
module and adds a function to configure that one.
I refactored service.js to this simplified version and it started to work.
var myApp = angular.module('myApp');
myApp.factory('securityService', function () {
return SomeFancyServiceObject();
});
这篇关于角模块配置不叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!