javascript 未调用 Angular 模块配置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15877925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 02:25:56  来源:igfitidea点击:

Angular module config not called

javascriptangularjs

提问by Bart

I'm trying to get my head around AngularJS and ran into a problem.

我试图了解 AngularJS 并遇到了问题。

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).

ng-app="myApp"<body>标签上有指令。并且以正确的顺序在正文结束标记之前加载脚本(就这点而言)。

    <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

更新:我的 service.js 的简化版本

'use strict';

angular.module('myApp', function ($provide) {
    $provide.factory('myService', function ($http) {
        var service = function () {
            ...
            service implementation
            ...
        };

        return new service();
    });
});

回答by Bart

It seems that the method I used for defining the service was overriding my myAppmodule.

我用于定义服务的方法似乎覆盖了我的myApp模块。

This is the overriding line

这是最重要的线

angular.module('myApp', function ($provide) ...

This code simply redefines the myAppmodule and adds a function to configure that one.

这段代码只是重新定义了myApp模块并添加了一个函数来配置那个模块。



I refactored service.js to this simplified version and it started to work.

我将 service.js 重构为这个简化版本,它开始工作了。

var myApp = angular.module('myApp');

myApp.factory('securityService', function () {
    return SomeFancyServiceObject();
});

回答by Illidan

I was missing ng-app="myApp" inside my html tag - and I realized this while reading your question :-)

我的 html 标签中缺少 ng-app="myApp" - 我在阅读您的问题时意识到了这一点:-)

回答by Nick

I usually see routing applied by chaining them together:

我通常会看到通过将它们链接在一起来应用路由:

$routeProvider
    .when('/dashboard', {templateUrl: 'partial/dashboard', controller: DashboardController})
    .when('/menu', {templateUrl: 'partial/other', controller: OtherController})
    .otherwise({redirectTo: '/dashboard'});

Can't see why yours would not work, but might be worth a go.

不明白为什么你的不起作用,但可能值得一试。