2017-07-26 24 views
0
加載頁面

我採用了棱角分明的路由器表是空白永遠 - 與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

您可以使用ng-if =「result」,然後在加載頁面時顯示結果 – Vivz

+0

您是否獲取數據並使用新數據更新視圖?你唯一的問題是桌子空了一會兒? –

+0

nope我的問題是,表是永遠是空的。頁面加載之前,我得到了響應使用$ HTTP –

回答

0

當路由器更改路由時,它將銷燬當前視圖的$範圍並使用控制器的新實例創建一個新的$範圍。

在新URL中包含新視圖需要的任何信息作爲查詢參數。

改變鏈接到包含參數:

<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; 
     }); 
     }; 
    }; 
}); 

在上面的例子中,控制器的新實例使用的當前PARAMS路由器加載必要的數據。