0

I have my app in Ionic and Angular. I have a list with checkbox items that come from an array. What I want is when the user clicks on a checkbox item, they are added to a separate list i.e my choices.

the code is: App.js

$scope.myList = [
{name:'Choice one'},
{name:'Choice two)'}
];

HTML VIEW:

  <div>
  <ul class="list">
    <li class="item item-checkbox" ng-repeat='item in myList | filter: search'>
  <label class="checkbox">
   <input type="checkbox" ng-model="option.myList[$index]">
  </label>
       <p ng-bind-html="item.name"></p>

    </li>
    <ul>
</div>

So I want the choices once clicked to be added to a list above which is similar code to the html above. Any ideas?

3
  • as far as i understand? you want to attach checkbox value (checked or not) to each object in myList ? Commented Sep 8, 2015 at 11:28
  • Yes but if u imagine the list of choices is seperate and once they click on a choice it is added to another list in a div above saying my Choices Commented Sep 8, 2015 at 11:30
  • Let me know if my answer helps. Commented Sep 8, 2015 at 12:04

2 Answers 2

0

maybe something like this:

$scope.myList = [
                    {name:'Choice one'},
                    {name:'Choice two)'}
                ];

$scope.myChoices = [];

$scope.stateChanged = function(checked){
    if(checked){
        $scope.myChoices.push(checked);
    }else{
        var index = $scope.myChoices.indexOf(checked);
        $scope.myChoices.splice(index,1);
    }
}

html:

<div>
    <ul class="list">
        <li class="item item-checkbox" ng-repeat='item in myList | filter: search'>
            <label class="checkbox">
                <input type="checkbox" ng-model="option.myList[$index]" ng-change='stateChanged(option.myList[$index])'>
            </label>
            <p ng-bind-html="item.name"></p>
        </li>
        <ul>
</div>
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Abduallah, what if I want it in a seperate div above this list?
Hi Abduallah, but for some reason the code above does not keep the ng-model="option.myList[$index]". (LocalStorage);
0

See this demo with your code : http://play.ionic.io/app/89d16b54285b

You do not need to make a seperate list to show selected options somewhere else. Just attach selected property with each object this way <input type="checkbox" ng-model="item.selected">

<ul class="list">
    <li class="item item-checkbox" ng-repeat='item in myList | filter: search'>
    <label class="checkbox">
    <input type="checkbox" ng-model="item.selected">
    </label>
       <p ng-bind-html="item.name"></p>
    </li>
<ul>

Now you can use same myList anywhere to see which is selected using filter

<div class="item" ng-repeat="item in myList | filter:{selected:true}">
  {{item.name}}
</div>

7 Comments

With the code above there is nothing for local storage? which I use with ng-model="option.myList[$index]". How would this be implemented also
Have you seen demo which i mentioned in my answer? and what do you mean by local storage?
I am using ng-model="option.myList[$index]" for localStorage how do i use this with your code?
as i told myList has both information now. Your datastructure will be [{name: 'choice1', selected: false},{name: 'choice2', selected: false}], now those object which are selected will have selected:true value. You can use myList also to store in localstorage. Point is you do not need to use extra array to store selected options.
Its working but can you show how you mean with the local storage?
|

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.