代码之家  ›  专栏  ›  技术社区  ›  Sanjalee Patni

AngularJS:局部视图未渲染

  •  3
  • Sanjalee Patni  · 技术社区  · 8 年前

    我正在尝试使用angular在单页应用程序中加载部分视图。我已经按照下面的脚本文件代码配置了路由。 以下是我加载局部视图的代码:

    var app = angular.module("myBlogApp", ["ngRoute"])
       .config(function ($routeProvider) {
           $routeProvider
           .when("/view1", {
               templateUrl: "Views/view1.html",
               controller: "view1Controller"
           })
          .when("/view2", {
              templateUrl: "Views/view2.html",
              controller: "view2Controller"
          })
           .when("/view3", {
               templateUrl: "Views/view3.html",
               controller: "view3Controller"
           })
       })
        .controller("view1Controller", function ($scope) {
            $scope.message = "You are in View 1";
        })
        .controller("view2Controller", function ($scope) {
            $scope.message = "You are in View 2";
        })
        .controller("view3Controller", function ($scope) {
            $scope.message = "You are in View 3";
        })
    

    以下是索引页:

    <html ng-app="myBlogApp">
    <head>
        <title></title>
        <meta charset="utf-8" />
        <script src="Scripts/angular.min.js"></script>
        <script src="Scripts/angular-route.min.js"></script>
        <script src="Scripts/Script.js"></script>
    </head>
    <body>
        <table>
            <tr>
                <td>
                    <a href="#/view1">View1</a>
                    <a href="#/view2">View2</a>
                    <a href="#/view3">View3</a>
                </td>
                <td>
                    <div ng-view></div>
                </td>
            </tr>
        </table>
    </body>
    </html>
    

    但当我加载索引时。html页面并单击超链接,我在我的页面上看不到视图。

    视图上的HTML为:

    <h1>View2</h1>
    <div>{{message}}</div>
    
    2 回复  |  直到 8 年前
        1
  •  1
  •   Aleksey Solovey    8 年前

    在AngularJS版本1.6中,他们更改了URL哈希bang的哈希前缀。链接现在有 #! 而不是 # . 更多信息: commit-aa077e8


    删除哈希前缀( ! ),您需要配置才能获得以下代码:

    $locationProvider.hashPrefix('');
    
        2
  •  1
  •   Manvendra Priyadarshi    8 年前

    <!DOCTYPE html>
    <html>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-route.js"></script>
    
    <body ng-app="myApp">
    
    
    <a href="#!view1">View 1</a>
    <a href="#!view2">View 2</a>
    <a href="#!view3">View 3</a>
    
    
    <div ng-view></div>
    
    <script>
    var app = angular.module("myApp", ["ngRoute"]);
    app.config(function($routeProvider) {
        $routeProvider
        .when("/view1", {
            template : "{{message}}",
            controller : "view1Controller"
        })
        .when("/view2", {
            template : "{{message}}",
            controller : "view2Controller"
        })
        .when("/view3", {
            template : "{{message}}",
            controller : "view3Controller"
        });
    });
    app.controller("view1Controller", function ($scope) {
        $scope.message = "You are in View 1";
    });
    app.controller("view2Controller", function ($scope) {
        $scope.message = "You are in View 2";
    });
    app.controller("view3Controller", function ($scope) {
        $scope.message = "You are in View 3";
    });
    
    </script>
    
    </body>
    </html>