gentos gentos - 5 months ago 26
Javascript Question

AngularJS $scope.form.$submitted shows true

I have a multi-step form and I want to control the user if he submitted a valid information in each step. I am checking for validation, and if true, i move to next step, but the $scope.form.$submitted shows True and the ng-message pops up.

function stepForward(step){
console.log($scope.AdvertiserRegistration.$valid);
if($scope.AdvertiserRegistration.$valid){
var t = setTimeout(function(){
$scope.AdvertiserRegistration.$submitted = false;
},3000);
window.location = '#/advertiser/register/step'+step;
}

}


So this is my function and it's not working:

console.log($scope.AdvertiserRegistration.$valid)
returns True.

Any solution?

Answer

When using angular any async operation takes you out of the angular digest cycle, so the angular team provides services for these operations, one of them is $timeout, change your code to this:

function stepForward(step){
            console.log($scope.AdvertiserRegistration.$valid);
            if($scope.AdvertiserRegistration.$valid){
                //YOU NEED TO INJECT $timeout
                var t = $timeout(function(){
                    $scope.AdvertiserRegistration.$submitted = false;
                },3000);
                window.location = '#/advertiser/register/step'+step;
            }

        }