2016-08-03 60 views
1

我是新來的angularjs和由於某種原因,我的路線不工作,當我點擊測試鏈接。我重複檢查了我的代碼,似乎無法找到問題。爲什麼不是我的路線加載?

任何幫助將非常感謝!

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
     <meta charset="UTF-8"> 
     <title>Title</title> 


     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script> 
     <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.9/angular-route.min.js"></script> 



     <style> 


     </style> 

    </head> 


    <body> 


    <a href="#test">Test</a> 

    <div ng-view></div> 


    <script> 
    var app = angular.module("myApp", ["ngRoute"]); 
    app.config(function($routeProvider) { 
     $routeProvider 
     .when("/test", { 
      templateUrl : "test.html" 
     }) 
    }); 
    </script> 

    </body> 
    </html> 
+1

你沒有給'NG-應用=「對myApp」'在HTML? – kukkuz

+0

作爲回答加入 – kukkuz

回答

1

有幾件事我想指出你錯過了什麼。

  1. 您錯過了在頁面上啓動角度。您可以通過執行ng-app="myApp"作爲myApp模塊來完成。
  2. 另一件事是,你必須改變你的hrefhref="#/test",以便在鏈接的點擊,您將瀏覽到/test航線
+1

Ahh好的。謝謝你的提示! – json3

0

視圖定義模塊名稱,

<body ng-app="myApp"> 

工作App

0

您必須添加ng-app="myApp"

<!DOCTYPE html> 
 
<html lang="en"> 
 

 
<head> 
 
    <meta charset="UTF-8"> 
 
    <title>Title</title> 
 

 

 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.9/angular-route.min.js"></script> 
 

 

 

 
    <style> 
 
    </style> 
 

 
</head> 
 

 

 
<body ng-app="myApp"> 
 

 

 
    <a href="#test">Test</a> 
 

 
    <div ng-view></div> 
 

 

 
    <script> 
 
    var app = angular.module("myApp", ["ngRoute"]); 
 
    app.config(function($routeProvider) { 
 
     $routeProvider 
 
     .when("/test", { 
 
      templateUrl: "test.html" 
 
     }) 
 
    }); 
 
    </script> 
 
    <script type="text/ng-template" id="test.html"> 
 
    Content of the template. 
 
    </script> 
 

 
</body> 
 

 
</html>