0

I want to show a spinner into button during function one running and after finishing, hide spinner, this is what I tried:

<div ng-controller="MyController">       
        <button ng-click="InsertData()">

            <i class="fa fa-refresh fa-spin" ng-show="loading"></i>Loading
        </button>
        <br />
        {{loading}}
    </div>

and this is controller.js

angular.module('MyApp', []).controller('MyController', function ($scope) {

$scope.InsertData=function()
{
    $scope.loading = true;
    $scope.one($scope.two);

}

$scope.one = function (callback) {

    setTimeout(function () { alert("this is function one"); callback(); }, 1000);

}

$scope.two = function () {
    alert("two");
    $scope.loading = false;

}

});

but this line

$scope.loading = false;

doesn't execute! although line above it runs, I mean alert("two") appears!

why the value of $scope.loading doesn't change in callback function? how to solve this problem?

0

1 Answer 1

0

The problem is because you are using setTimeout which is a Javascript function, this will cause issues, where the scope variable will not be updated in the scope. Hence we must use the angular wrapper for this which is $timeout().

More details here, to know the difference between $timeout() and setTimeout() refer here


Below is a working fiddle for the question.

JSFiddle Demo

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.