簡體   English   中英

Angular模塊配置未調用

[英]Angular module config not called

我試圖繞過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);
 }]);

據我所知,在加載模塊時應該調用配置函數。 但事實並非如此!

我在<body>標簽上有ng-app="myApp"指令。 並且腳本在正文關閉標記之前以正確的順序加載(至關重要)。

    <script src="angular.js"></script>      
    <script src="app.js"></script>
    <script src="services.js"></script>
    <script src="controllers.js"></script>
</body>

我很確定我必須在這里忽視一些微不足道的事情。 應該在控制台上打印一條消息。 但它仍然是空的,沒有任何錯誤或警告。

應用程序的部分不依賴於路線運行得很好。

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

'use strict';

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

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

似乎我用於定義服務的方法覆蓋了我的myApp模塊。

這是最重要的路線

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

此代碼只是重新定義myApp模塊並添加一個函數來配置該模塊。


我將service.js重構為這個簡化版本,它開始工作。

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

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

我在html標簽中缺少ng-app =“myApp” - 我在閱讀你的問題時意識到這一點:-)

我通常看到通過將它們鏈接在一起來應用路由:

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

不明白為什么你的工作不行,但可能值得一試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM