2016-03-05 18 views
0

我有這個奇怪的問題 - 我使用Angular的ngRoute作爲我的應用程序。 我對模板不同的控制器,如:Angular.js - 控制器沒有執行索引,適用於其餘模板

routes.js:

angular.module('PokeApp', ['ngRoute']) 
    .config(function($routeProvider){ 
    $routeProvider 
    .when('/', { 
     templateUrl: '../templates/template-index.html', 
     controller: 'IndexController' 
    }) 
    .when('/products', { 
     templateUrl: '../templates/template-products.html', 
     controller: 'ProductsController' 
    }) 
    .when('/events', { 
     templateUrl: '../templates/template-events.html', 
     controller: 'EventsController' 
    }) 
    .otherwise({ redirectTo: '/' }); 

控制器都包含在index.html作爲如下:

<!-- Scripts --> 
<script src="js/angular.min.js"></script> 
<script src="js/angular-route.min.js"></script> 

<script src="js/routes.js"></script> 

<script src="js/controllers/controller-index.js"></script> 
<script src="js/controllers/controller-products.js"></script> 
<script src="js/controllers/controller-events.js"></script> 

我的控制器實際上是在same - ill向您顯示索引和產品,例如:

controller-index.js:

angular.module('PokeApp') 
.controller('IndexController', function($scope) { 
    // for testing purposes 
    console.log("hi"); 

    .. 

}); 

控制器products.js:

angular.module('PokeApp') 
.controller('ProductsController', function($scope) { 
    // again, for testing 
    console.log("hi"); 

    .. 

}); 

但這裏的怪異的一部分 - 當我到了「/」頁面,我沒有得到控制器日誌到控制檯。沒有錯字,因爲如果我改變控制器的名稱爲錯誤的,我在控制檯中得到錯誤。

但是當我去/產品,我得到'嗨'如預期。

我檢查了我能想到的所有東西 - 錯別字,錯誤的位置,stackoverflow的線程。我只是不知道爲什麼索引控制器拒絕工作,而其他人(我有更多)按預期工作。

任何人都可以幫我解決這個問題嗎?謝謝你的時間。

+0

查看瀏覽器開發工具網絡,查看模板請求是否失敗。 – charlietfl

+0

文件正在成功接收,我在那裏檢查 – Zephyer

回答

0

你可以嘗試的,否則情況下添加到您的路線提供商: otherwise({ redirectTo: '/' });

+0

你們,我忘了在這裏顯示那部分,但.otherwise在我的文件 – Zephyer

0

您可以指定這樣

$routeProvider 
    .when('/index', { 
     templateUrl: '../templates/template-index.html', 
     controller: 'IndexController', 
    }) 
    .otherwise({ redirectTo: '/index' }); 


默認路由,但我建議你使用 ui-router從開始你的項目。 它支持ngRoute的正常功能以及許多額外的功能。

+0

你們,我忘了在這裏顯示那部分,但.otherwise是在我的文件中。 雖然我會檢查UI-Router,謝謝! – Zephyer