0

Following is my angular controller code -

$scope.visibility = true;
$http.get("/api/getempl").then(function(response){
    if (response.status == 200) {
        $scope.empData = response.data.data;
        (function(){
            setTimeout(function(){
                $scope.visibility = false;
            }, 4000);
        })();
    }
});

What I am trying is to show Loading Image till data not fetched and flagging $scope.visibility on response basis -

.col-md-12(ng-show="visibility")
  h1 LOADING
.col-md-12(ng-show="!visibility")
  // DATA IN TABLE

Now LOADING text is always visible as I am expecting once data fetched, after a delay show table data but its not working, let me know what I am doing wrong here.

2
  • Why not just show the data when it's available, what makes you think your users want to wait another 4 seconds ? Commented Sep 9, 2014 at 10:43
  • @adeneo actually when I am pouring the data in the table there is a little flickering for some time and then every thing sets..so I am making a delay..4 sec delay is just for the testing purpose Commented Sep 9, 2014 at 10:46

2 Answers 2

2

use $timeout

Angular's wrapper for window.setTimeout. The fn function is wrapped into a try/catch block and delegates any exceptions to $exceptionHandler service.

$scope.visibility = true;
$http.get("/api/getempl").then(function(response){
    if (response.status == 200) {
        $scope.empData = response.data.data;

            $timeout(function(){
                $scope.visibility = false;
            }, 4000);

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

2 Comments

$timeout is asynchron function , what you mean ?
My bad ..I need to inject $timeout in my controller..thx +1 and green tick :)
1

Use $timeout instead of setTimeout:

$scope.visibility = true;
$http.get("/api/getempl").then(function(response){
    if (response.status == 200) {
        $scope.empData = response.data.data;
        $timeout(function(){
            $scope.visibility = false;
        }, 4000);
    }
});

See the working example: jsFiddle example

1 Comment

My bad ..I need to inject $timeout in my controller..thx +1

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.