表永远是空白的 - 使用 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文件,然后在函数返回结果时再次加载?

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

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

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

最新更新