2016-11-05 28 views
0

什麼似乎是錯誤的,我沒有得到任何錯誤,但在同一時間路由沒有進展。 。angularjs路由不起作用沒有發現錯誤

的index.html

<!DOCTYPE html> 
<html> 
<head> 
<title>My Angular App</title> 
<link rel="stylesheet" type="text/css" href="/myAngularApp/css/style.css"> 
<link rel="stylesheet" type="text/css" href="/myAngularApp/css/bootstrap/css/bootstrap.min.css"> 
<script type="text/javascript" src="/myAngularApp/css/angular/angular.min.js"></script> 
<script src="/myAngularApp/css/mainpage.js"></script> 
<script type="text/javascript" src="/myAngularApp/css/angular/angular-animate.min.js"></script> 
<script type="text/javascript" src="/myAngularApp/css/angular/angular-resource.min.js"></script> 
<script type="text/javascript" src="/myAngularApp/css/angular/angular-route.min.js"></script> 
<script type="text/javascript" src="/myAngularApp/css/bootstrap/ui-bootstrap-tpls-2.2.0.min.js"></script> 

</head> 
<body ng-app="myApp"> 

<h1> this is index.html </h1> 

<div ng-view></div> 

</body> 
</html> 

mainpage.js //在我的控制器是

var app = angular.module('myApp', ['ngRoute', 'ngAnimate', 'ngResource', 'ui.bootstrap']); 

app.config(function($routeProvider) { 
    $routeProvider 
    .when('/', { 
     templateUrl : 'index.html' 
    }) 
    .when('/home', { 
     templateUrl : 'homepage.html' 
    }) 
    .otherwise({ 
     templateUrl : 'index.html' 
    }); 

}); 

當我加載 '/' 在我的瀏覽器nothins正在發生同樣以/ home我不知道在哪裏我沒有錯誤,因爲我的控制檯沒有錯誤

回答

0

路線的模板應該是局部視圖。這裏index.html是單頁面應用的根,這個頁面不應該重新加載(除非你想銷燬並重新啓動應用)。嘗試更改templateUrl for /到一個文件,只是有<h2> HELLO WORLD!</h2>和監視控制檯(開發工具)與獲取模板相關的任何404錯誤

+0

謝謝先生!有用 :) –

0

我認爲問題在於您的JavaScript文件名。在索引中,您提到了mainpage.js,並在描述其homepage.js中。

編輯:好的。然後嘗試安排文件列表像

<script type="text/javascript" src="/myAngularApp/css/angular/angular.min.js"></script> 
<script type="text/javascript" src="/myAngularApp/css/angular/angular-animate.min.js"></script> 
<script type="text/javascript" src="/myAngularApp/css/angular/angular-resource.min.js"></script> 
<script type="text/javascript" src="/myAngularApp/css/angular/angular-route.min.js"></script> 
<script type="text/javascript" src="/myAngularApp/css/bootstrap/ui-bootstrap-tpls-2.2.0.min.js"></script> 
<script src="/myAngularApp/css/mainpage.js"></script> 
+0

opsss對不起,打字錯誤,應該是mainpage.js我的不好 –

0

如由用戶普里的答覆中提到,文件名可以是問題。但是,這應該會在控制檯上產生錯誤,並且根據您的說法,控制檯上沒有錯誤。

其他可能的原因可能是您導入文件的順序。我一般序列我的文件這樣 -

<head> 
 
    <!-- 1. External CSS Libraries likes bootstrap, jquery --> 
 
    
 
    <!-- 2. My Application specific CSS files --> 
 
    
 
    <!-- 3. External JS libraries like Angular, bootstrap, JQuery --> 
 
    
 
    <!-- 4. My Application specific JS files like my controllers, services, directives etc. 
 
</head>

同樣的順序在其中引入外部JS庫也可能會導致問題。

+0

我試着做你建議先生,但問題是我每次加載'/'我總是得到這個'#/'? –

+0

與'/'對應的視圖是否正確加載?角度js中的所有URL將默認爲這樣 - http:// yourHost:port /#/ yourRoute/anyPathParams –

+0

nope。每當我點擊「本地主機/ myAngularApp /」我得到「本地主機/ myAngularApp /#/」,但index.html的內容是出現在瀏覽器 –