如何在 AngularJS 从数据库获得响应后加载 HTML 页面?



我在HTML页面上有一个表格,其中数据存储在数据库中。我希望仅在收到数据后加载表或整个页面。我可以这样做吗?

AngularJS 代码:

angular.module("app", []).controller("controller", function($scope, $http) {         
$http({
method: 'POST',
url: 'updateCoins.php',
data: {get: 1},
}).then(function(response){
$scope.db = response.data;
});
});

表格填满如下:

<tr>
<td>{{db[0]['percent']}}%</td>
</tr>

通常,您会对路由器使用解析。你也可以做这样的事情:

angular.module("app", [](.controller("controller", function($scope, $http( {

$scope.dataLoaded = false        
$http({
method: 'POST',
url: 'updateCoins.php',
data: {get: 1},
}).then(function(response){
$scope.db = response.data;
$scope.dataLoaded = true
});
});

然后在加载控制器的最外层元素上的 html 中使用 ng-if="dataLoaded">

如果你没有使用ngRoute或ui.router和解析,最简单的方法是在之前设置一个变量。例子:

angular.module("app", []).controller("controller", function($scope, $http) {         
$scope.hasCompleted = false;
$http({
method: 'POST',
url: 'updateCoins.php',
data: {get: 1},
}).then(function(response){
$scope.db = response.data;
$scope.hasCompleted = true;
});
});
<tr>
<td ng-show="hasCompleted">{{db[0]['percent']}}%</td>
</tr>

最新更新