0

I have been wandering for two to three hours and I found chunks of relevant questions but my scenario just take a little curve. I have to generate a number of checkboxes which obviously would be generated through ng-repeat. How can I show the preselected values which I am getting from Api. Here is the kind of data I am receiving.

Pre-selected data

$scope.categoriess = [{"id":1,"name":"Garden Cleaning"},{"id":3,"name":"Home Cleaning"}].

and this is the data over which I am using ng-repeat.

ng-repeat data

$scope.categories = [{"id":1,"name":"Garden Cleaning"},{"id":2,"name":"Gutter Cleaning"},{"id":3,"name":"Home Cleaning"},{"id":4,"name":"Pool Cleaning"}

HTML

  <div ng-repeat="cats in categories">
      <input type="checkbox" ng-model="categoriess.id[cats.id]">
               <label>{{cats.name}}</label>
                  </div>

Now how would i tell ng-repeat to check the preselected data, plus if I want to check few more boxes they should also be checked and I want them to store in $scope.categoriess. I have tried a number of solution either with ng-checked or by make a function call in ng-checked I got no expected results.

3 Answers 3

1

Why don't u format your data to have another property, something like

{"id":1,"name":"Garden Cleaning", "checked": true}

Such way, when u iterate through the data, you can check the checkbox

<div ng-repeat="cats in categories">
  <input type="checkbox" ng-checked="cats.checked">
  <label>{{cats.name}}</label>
</div>
Sign up to request clarification or add additional context in comments.

2 Comments

thats why i posted this question, because it is what it is .
There should be some references, if you know the default categories., then u can call a function in ng-checked to match the id and return true, but not redundant though!!
1

I saw this but i did not know how it works god it saved my life. see this checklist-model. Its so simple , no need to use ng-checked or something dynamic , preselected and gets updated easily

[1]: http://jsfiddle.net/iem_usman/v3k6pwrj/1/

Comments

0

Change your html like this :

<div ng-repeat="cats in categories">
   <input type="checkbox" ng-model="categoriess.id[cats.id]" ng-checked="isChecked(cats.id);">
   <label>{{cats.name}}</label>
</div>

Add this function in your controller :

$scope.selectedCategoriess = [{"id":1,"name":"Garden Cleaning"},{"id":3,"name":"Home Cleaning"}].

$scope.isChecked = function(id){
for (var i = 0; i < $scope.selectedCategoriess.length; i++) {
    if ($scope.selectedCategoriess[i].id == id) {
        return true;
    }
}
return false;
}

If id is available checked value will be "true".

5 Comments

what does this line means ? ng-model="categoriess.id[cats.id]"
It's in your code, I only added ng-checked function
There was an error coming on this line for (var i = 0; i < $scope.selectedCategoriess.length; i++) length of undefined, when i set $scope.selectedCategoriess as dynamic but i handle it and fixed it, by writting if($scope.selectedCategoriess){for......}
I tried your solution before, but at that time i did not use ng-model="categoriess.id[cats.id]"
I have implemented same solution in my application, works fine.

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.