无法在AngularJS中以HTML5模式切换视图

时间:2015-07-08 23:23:30

标签: javascript angularjs html5

我正在使用this code来使其与HTML5链接一起使用。不知何故,我无法使其工作:视图未解析为URL。另外,我不清楚我是否需​​要把" /"在视图名称前面。如果我这样做,浏览器URL链接会显示" file:/// addStudent" ,没有任何基础,另一方面,如果我没有,我会看到带有base的URL。这应该是那样的吗?在任何情况下,我都会看到错误消息:"找不到此网页" 。我在这做错了什么?谢谢!

testAngularJS.htm

<html>
<head>
   <title>Angular JS Views</title>
   <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
   <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular-route.min.js"></script>
   <base href="file:///home/myusername/projects/parser_ui/">
</head>
<body>
   <h2>AngularJS Sample Application</h2>
   <div ng-app="mainApp">
      <p><a href="addStudent">Add Student</a></p>
      <p><a href="viewStudents">View Students</a></p>
      <div ng-view></div>
      <script type="text/ng-template" id="addStudent.htm">
         <h2> Add Student </h2>
         {{message}}
      </script>
      <script type="text/ng-template" id="viewStudents.htm">
         <h2> View Students </h2>       
         {{message}}
      </script> 
   </div>

   <script>
      var mainApp = angular.module("mainApp", ['ngRoute']);

      mainApp.config["$locationProvider", "$routeProvider", function($routeProvider, $locationProvider) {

            // use the HTML5 History API
            $locationProvider.html5Mode(true);

            $routeProvider.
               when('/addStudent', {
                  templateUrl: 'addStudent.htm',
                  controller: 'AddStudentController'
               }).
               when('/viewStudents', {
                  templateUrl: 'viewStudents.htm',
                  controller: 'ViewStudentsController'
               }).
               otherwise({
                  redirectTo: 'addStudent'
               });
      }];

      mainApp.controller('AddStudentController', function($scope) {
         $scope.message = "This page will be used to display add student form";
      });

      mainApp.controller('ViewStudentsController', function($scope) {
         $scope.message = "This page will be used to display all the students";
      });
   </script>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

Plunkr中运行但不在本地运行:

&#13;
&#13;
<!DOCTYPE html>
<html>
<head>
   <title>Angular JS Views</title>
   <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
   <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular-route.min.js"></script>
   <base href="/">
</head>
<body>
   <h2>AngularJS Sample Application</h2>
   <div ng-app="mainApp">
      <p><a href="/addStudent">Add Student</a></p>
      <p><a href="/viewStudents">View Students</a></p>
      <div ng-view></div>
      <script type="text/ng-template" id="addStudent.htm">
         <h2> Add Student </h2>
         {{message}}
      </script>
      <script type="text/ng-template" id="viewStudents.htm">
         <h2> View Students </h2>       
         {{message}}
      </script> 
   </div>
   <script>
      var mainApp = angular.module("mainApp", ['ngRoute']);

      mainApp.config(["$locationProvider", "$routeProvider", function($locationProvider, $routeProvider) {

            // use the HTML5 History API
            $locationProvider.html5Mode(true);

            $routeProvider.
               when('/addStudent', {
                  templateUrl: 'addStudent.htm',
                  controller: 'AddStudentController'
               }).
               when('/viewStudents', {
                  templateUrl: 'viewStudents.htm',
                  controller: 'ViewStudentsController'
               }).
               otherwise({
                  redirectTo: '/addStudent'
               });
      }]);

      mainApp.controller('AddStudentController', function($scope) {
         $scope.message = "This page will be used to display add student form";
      });

      mainApp.controller('ViewStudentsController', function($scope) {
         $scope.message = "This page will be used to display all the students";
      });
   </script>
</body>
</html>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

好的,我最终能够让它发挥作用。所以这是  需要做什么:

  1. 必须在服务器上运行,而不是在本地文件(file:///)上运行。我在上面的评论中使用了由Phil建议的带有nodejs的节点。
  2. 在这一行(下面)我删除了括号,因为使用 file:/// 它给了我一个错误。由于SO的其他一些建议,我做到了。但是,括号必须在那里,否则,它将无法与服务器一起使用。

    mainApp.config["$locationProvider", "$routeProvider", function($routeProvider, $locationProvider) {
    
  3. ......应该是:

       mainApp.config(["$locationProvider", "$routeProvider", function($routeProvider, $locationProvider) {
    
    1. 由于AngularJS是一个SPA框架,您可能还会发现需要配置服务器以将所有URL传递给Angular,以便将所有 / 解释为视图而不是文件夹/文件。
    2. 例如,在nodejs中我添加了以下内容:

      app.use('/*', function(req, res){
        res.sendFile(path.resolve(__dirname + '/public/angular/views/index.html'));
      });
      

      话虽如此,当我评论上述情况时,它仍然运作良好,我不知道为什么。