0

I have created an button as:

<ion-radio ng-repeat="business in multipleBusiness track by business.id" ng-model="choice.value" ng-value="business">{{business.name}}</ion-radio>

and In controller I am doing...

$scope.choice.value = localStorageService.get('defaultBusiness') || $scope.multipleBusiness[0];

Now the radio buttons are shown with a checkmark when the value passed to $scope.choce.value is $scope.multipleBusiness[0] but when the value from localStorage is used i.e. localStorageService.get('defaultBusiness') , the chekmark is not shown

Even though on consoling both the values are same structure i.e objects with id and name keys.

Dean Ambrose
  • 183
  • 10

1 Answers1

0

The localStorageService.get('defaultBusiness') will return string. You need to use JSON.parse() to have it as an json object.

Shashank Vivek
  • 16,888
  • 8
  • 62
  • 104
  • but typeof is giving the output from localStorage as object. – Dean Ambrose May 09 '18 at 06:55
  • @DeanAmbrose Check https://plnkr.co/edit/b0hLbSmhxux2n6QXfuuk?p=preview. Refresh and see the console after selecting a value and see it for yourself. – Shashank Vivek May 09 '18 at 07:04
  • @DeanAmbrose Did my suggestion worked in your case or not ? please confirm. – Shashank Vivek May 09 '18 at 07:05
  • no...because you are stringifying the data before saving and then converting it back to json after getting from local storage. I have directly saved it to local storage as object hence I don't think I need to JSON,parse() as the data I am receiving is already in json form. – Dean Ambrose May 09 '18 at 07:10
  • You don't need to stringify with this library (angular-local-storage) , because the set function utilizes angular's toJson() method under the hood. Similarly, the get method JSON.parse()s the item to return. No need for that either. – Dean Ambrose May 09 '18 at 07:16
  • @DeanAmbrose I stringified because localstorage doesn't support `objects`. https://stackoverflow.com/questions/2010892/storing-objects-in-html5-localstorage . Share a plunkr so that I can see what exactly you have implemented – Shashank Vivek May 09 '18 at 07:18