表永远是空白的 - 使用ngRoute加载页面

时间:2017-07-26 09:27:00

标签: html angularjs ngroute

我正在使用角度路由器

app.config(function($routeProvider) {
    $routeProvider
        .when('/comment/list', {
        templateUrl: 'commentList.htm',
        controller: 'mainController'
    })
});
<td ng-show="btnUADetails" ng-click="loadPage('commentList', x.userName);">
           <a class="detailButton" href="#/comment/list"></a>                   
</td>

这是我的角度函数

$scope.loadPage = function(pageId, id) {
     if (pageId == "commentList") {
         $scope.getServerData($scope.generateUrl(pageId, 'json', id)).then(function(result) {
             $scope.serverComment = result;
         });
     } else {
         //do something
     }
 }

在$ HTTP返回响应html页面加载之前,我在html表中获得干净的数据。我的函数返回结果后可以加载此页面吗?或者首先加载html文件并在函数返回结果时再次加载它?

1 个答案:

答案 0 :(得分:0)

当路由器更改路由时,它会销毁当前视图的$ scope并使用新的控制器实例创建一个新的$ scope。

在新网址中包含新视图作为查询参数所需的任何信息。

更改链接以包含参数:

<td ng-show="btnUADetails" ̶n̶g̶-̶c̶l̶i̶c̶k̶=̶"̶l̶o̶a̶d̶P̶a̶g̶e̶(̶'̶c̶o̶m̶m̶e̶n̶t̶L̶i̶s̶t̶'̶,̶ ̶x̶.̶u̶s̶e̶r̶N̶a̶m̶e̶)̶;̶"̶ >
    <a class="detailButton" href="#/comment/list?id={{x.userName}}"></a>
</td>

使用控制器中的参数:

app.controller("mainController", function($scope, $route) {
    this.$onInit = function() {
      if ($route.current.templateUrl == 'commentList.htm') {
         var pageId = 'comment';
         var id = $route.current.params.id;
         $scope.getServerData($scope.generateUrl(pageId, 'json', id))
           .then(function(result) {
             $scope.serverComment = result;
         });
      };
    };
});

在上面的例子中,控制器的新实例使用路由器的当前参数来加载必要的数据。