0

I am fetching data from LocalStorage as follows

angular.module('madMeApp').controller('campaignInDetails', function($scope) {
  var campaignToShow = JSON.parse(localStorage.getItem("CampaignDetails"));
  $scope.selectedDays = campaignToShow.selected_days;
});

I am getting selected_days value in a comma separated string e.g. Tuesday,Wednesday

Based on that value of selected_days

I want to be checked the below Checkbox,

<input type="checkbox" value="Monday" name="selected_days" />
<input type="checkbox" value="Tuesday" name="selected_days" />
<input type="checkbox" value="Wednesday" name="selected_days" />
<input type="checkbox" value="Thursday" name="selected_days" />
<input type="checkbox" value="Friday" name="selected_days" />
<input type="checkbox" value="Saturday" name="selected_days" />
<input type="checkbox" value="Sunday" name="selected_days" />

Kindly help me that how to check the checkbox using AngularJs as per the value. In this condition I want to be checked Tuesday,Wednesday checkbox.

Thanks.

4
  • multiple input of type checkbox with the same name? I thought you only did that for input of type radio Commented May 25, 2017 at 6:54
  • Yes Jaromanda these are in a same group so name of these checkboxes are same. Users can check multiple checkbox. Commented May 25, 2017 at 7:00
  • @Ajeet have you tried my answer? Commented May 25, 2017 at 7:07
  • Yes @the_mishra :) I am just trying, its really cool and will work. Will update after trying. Thanks... Commented May 25, 2017 at 7:12

1 Answer 1

1

create a function for ng-checked which would return true or false based on you local storage string. example :

<input type="checkbox" value="Monday" name="selected_days" ng-checked="dayChecker("Monday")" />

In controller

var str = "Tuesday,Wednesday";
$scope.dayChecker = function(day){
  if(str.indexOf(day) > 0){
    return true;
  } else{
    return false;
  }
}

Hope it would help

Sign up to request clarification or add additional context in comments.

Comments

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.