简体   繁体   中英

How to store values from ng-dropdown-multiselect in sessionStorage in AngularJS?

I am looking for a simple way to save and load data to / from session storage when it comes to multiselect dropdown. I'm using AngularJS.

Saving and loading data happens on a page reload.

HTML:

<div class="select-wrapper">
       <p class="par-multiselect-title">Items to choose from: </p>
       <div ng-dropdown-multiselect="" options="items" selected-model="selectedItems" extra-settings="mySettings"></div>
</div>

Here is how I solved it when it comes to single text input or checkbox. I can't however crack multiselect dropdown.

sessionStorage.setItem("name", $('#name').val());
sessionStorage.setItem("isNew", document.getElementById("isNew").checked);

if (sessionStorage.getItem("name")) {
    $scope.itemName = sessionStorage.getItem("name");
} 

var isNewChecked = JSON.parse(sessionStorage.getItem("isNew"));
if (isNewChecked == true) {
    document.getElementById("isNew").checked = isNewChecked;
} 

If sessionStorage.setItem supports string only, you can stringify the json:

$scope.selectedItems = [/* ... */];
sessionStorage.setItem("selectedItems", JSON.stringify($scope.selectedItems));

and load:

$scope.selectedItems = JSON.parse(sessionStorage.getItem("selectedItems"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM