Error: [$rootScope:inprog] $digest already in progress, solution $rootScope.apply() not working

Here is my code in Login Controller…

    if(!AppGlobal.hasConnection())

{
            $rootScope.connectionErrMsg();
        }
        else if(!loginError)
        {
            $rootScope.showLoading();
            
            $http({
                method:‘post’,
                url:HOST+’/auth/login’,
                data:{
                    email:$scope.loginForm.email,
                    password:$scope.loginForm.password,
                }
            }).success(function(resMat){
                
                var response=angular.fromJson(resMat);
                console.log(response);
                if(response.status==“login_success”)
                {    
                    localStorage.setItem(“UserId”,response.user_id);
                    localStorage.setItem(“LoginToken”,response.loginToken);
                    AppGlobal.appUserId=response.user_id;
                    AppGlobal.appLoginToken=response.loginToken;
                    alert(AppGlobal.appUserId);
                    alert(AppGlobal.appLoginToken);
                }
                else
                {
                    $rootScope.alertMsg(response.errorMsg,“Login Alert”,“OK”);
                }
                
                $rootScope.hideLoading();
            }).error(function(){
                $rootScope.hideLoading();
            });
        }

i am getting Error: [$rootScope:inprog] $digest already in progress ?
What should i do ?
i tried $rootScope.apply() but its not working

I am sharing the topi called "Solution to “Error: $digest already in progress” in AngularJS, please go through it, may be you will find it helpful…
Solution to “Error: $digest already in progress” in AngularJS

Heyho,

using .apply is in most cases a bad idea.
If there is already a refreshing cycle processing in the background, you will get exactly this error.

use $timeout(function () {}, 0); to hang into the next cycle.
Additional to that you read something about $digest.

https://docs.angularjs.org/guide/scope