Search code examples
angularjs2-way-object-databinding

2 way binding not working in setTimeout callback in angular 1


I want to disable submit button during save and enable it after saved.

It is not enabling. I can see the flag changed. But the flag on the view is not updating as I would imagine 2 way binding should give me that.

The code as below

$scope.save = function (){
    $scope.saving_survey = true;
    setTimeout(function(){
        $scope.saving_survey = false;
        console.log($scope.saving_survey);
    }, 1000);

}


<button class="btn btn-success" ng-click="save()" ng-disabled="saving_survey">
    save {{saving_survey}}
</button>

Solution

  • Try use $timeout service. it should be injecte to controller

     $scope.save = function (){
      $scope.saving_survey = true;
       $timeout(function(){
        $scope.saving_survey = false;
        console.log($scope.saving_survey);
      }, 1000);
    }