AngularJS:部分视图未渲染

3
我将尝试使用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>

但是当我加载index.html页面并点击超链接时,我的页面上没有看到视图。
视图上的HTML代码如下:
<h1>View2</h1>
<div>{{message}}</div>

这些视图中是否有message绑定? - Sajal
这是视图的代码:<h1>View2</h1> <div>{{message}}</div> - Sanjalee Patni
你使用的是哪个版本的AngularJS?你可能需要使用href="#!/view1"而不是href="#/view1"。同时,检查控制台日志以查找错误(也许是路径错误)。 - Aleksey Solovey
@AlekseySolovey 是的,这解决了问题。谢谢 :) - Sanjalee Patni
@Lee,你也可以尝试使用ng-href,我认为你不需要在那里添加#! - Aleksey Solovey
2个回答

1
在AngularJS 1.6版本中,URL哈希前缀被更改为哈希-爆炸。链接现在使用#!而不是#。有关详细信息,请参见:commit-aa077e8
为了去掉哈希前缀(!),您需要在配置中添加以下代码:
$locationProvider.hashPrefix('');

使用了 #!view1 而不是 #!/view1(省略了斜杠)。这也可以工作。有什么特别的原因吗? - Sanjalee Patni
@Lee 不确定,但可能取决于 $routeProvider.when() 的工作方式(因为它无论出于何种原因都会忽略 /)。 - Aleksey Solovey

1

<!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>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接