0

I am new in angular js and implementing single page application. I have implemented one checkbox and when I checked checkbox then I want to persist status of the checkbox after reloading. So When I click on the checkbox then storing the status(checked or unchecked) in session through $on($destroy) in Angular js.

I have seen one link Keep checkbox checked after page refresh AngularJS, But this is not working in my case.

I am sharing my sample code.

HTML

 <input type="checkbox" id="mycheckbox" class="checkbox style-0" name="mycheckbox" ng-model="mycheckbox" data-ng-change="confirm()" autocomplete="off"/>

CONTROLLER:

$scope.confirm = function(){
  if($scope.mycheckbox== false)
  {
    sessionStorage.setItem("confirmCheck", $scope.mycheckbox);        
  }
  if($scope.mycheckbox== true){
    sessionStorage.setItem("confirmCheck", $scope.mycheckbox);       
  }
}
if(sessionStorage.confirmCheck == "true"){      
   $('#mycheckbox').prop('checked',true);
}else {
   $('#mycheckbox').prop('checked',false);
}

$scope.$on('$destroy',function(){
  sessionStorage.confirmCheck=sessionStorage.getItem('confirmCheck');
     })

enter image description here

I am able to storing the status of checkbox in session, But I can't able to change status in HTML page.When I navigate to another page and return back to previous page then value is persist in angular controller but not updated in HTML page. Please share your idea. thanks in Advance.

6
  • When inspecting the dom, what value does sessionStorage.confirmCheck have? is it True or "true" ? Commented Jul 23, 2017 at 17:08
  • When Checkbox is clicked then true and unchecked the false Commented Jul 23, 2017 at 17:09
  • But when You clicked the checkbox then in javascript showing true in console .dubugger and persist value in javascript until unchecked from html page. But For unchecked checkbox, I have to checked and then after unchecked for false value. Commented Jul 23, 2017 at 17:10
  • yeah but does your session store the string "true" or the bool value true? try to change the check to sessionStorage.confirmCheck == true instead and see if it dosnt work Commented Jul 23, 2017 at 17:11
  • @MathiasRønnowNørtoft thanks,, But I have tried this one, it is not working. Commented Jul 23, 2017 at 17:17

1 Answer 1

1

I really suggest you to avoid mixing angular with jQuery, instead of that you will prefer to get the job done with angular.

1st) Save session storage checked values in a place you can access from angular template.

$scope.isChecked = sessionStorage.getItem('confirmCheck');

2nd) Use angular directive ngChecked.

<input type="checkbox" ng-checked="isChecked"></input>
Sign up to request clarification or add additional context in comments.

5 Comments

Eduardo Thanks for this answer. I am new in Angular. Could you provide me HTML with Directive. Or Above both steps should I follow?
Sure, is a simple example. You can see this on fiddle jsfiddle.net/6pr17t77
thanks a lot. it is working. But it is by default checked after reloading so If I unchecked then checkbox should be unchecked.
Yes, you need to read the sessionStorage variable and save on isChecked variable from example.
thanks a lot giving your time. thanks. I will implement.

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.