2016-08-03 63 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>