4

I have a table on an HTML page where the data is stored in a database. I want the table or the whole page to be loaded only after the data is received. Can I do this?

AngularJS Code:

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

The table gets filled like this:

<tr>
    <td>{{db[0]['percent']}}%</td>
 </tr>
2
  • Is this not working in some way? Is the AJAX call successful? What's in response? Commented Jul 25, 2017 at 18:48
  • Just read the docs about ngIf ngHide ngShow, too basic Commented Jul 25, 2017 at 18:50

2 Answers 2

2

Generally you'd use a resolve with your router for this. You can also do something like this:

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

Then in your html on the outermost element where you load your controller use ng-if="dataLoaded"

Sign up to request clarification or add additional context in comments.

1 Comment

It doesn't work if I put ng-if="dataLoaded" in the element where I user ng-controller (the whole page is blank), but works if I do it for the element directly below it.
2

Easiest way if you're not using ngRoute or ui.router with resolves is to have a variable set before. Examples:

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.