0

I have a simple array of objects:

pickedFeeds = [
  {
    "id": "1",
    "name": "ALLEN Production Lake",
    "status": "IN_PROGRESS",
    "owner": "ih546q"
  },
  {
    "id": "2",
    "name": "Demo Lake",
    "status": "IN_PROGRESS",
    "owner": "ih546q"
  }
]

And then i have a list of "feeds" like this:

feeds:[{
                id:1,
                name:'ALLEN Production Lake'
            },{
                id:2,
                name:'Demo Lake'
            },
            {
                id:3,
                name:'mamalake'
            },
            {
                id:4,
                name:'tatalake'
            },
            {
                id:5,
                name:'mapalake'
            },
            {
                id:6,
                name:'gagalake'
            },
            {
                id:7,
                name:'ramalake'
            },
            {
                id:8,
                name:'chakalake'
            },
            {
                id:9,
                name:'dumdumlake'
            },
            {
                id:10,
                name:'popolake'
            }]
        };

on the last list i have a checkboxes on them:

<div ng-repeat='feed in feeds'>
    <input  type="checkbox" id='{{feed.id}}'/>
                          <label for='{{feeds.id}}' class='lake-name'>{{feed.name}}<span class='checkbox-input-style'></span></label>

I want to do the following:

i want to check[V] the ones who are allready in the pickedfeeds array, when i uncheck i want to remove, if exist from the list of pickedFeeds and so forth.. How should i do so? what is my ng-model should be?

1

1 Answer 1

1

You can create function to check if in pickedFeeds exist element with this same id as feed.id ie:

$scope.test = function(feed){    
   if(filterFilter($scope.pickedFeeds,{id:feed.id} ).length >0)
     {       
       return true;       
     }    
  }

and after that you can add to your view :

<input  type="checkbox" id='{{feed.id}}' ng-model="feed.checked" ng-checked="test(feed)" />

Just don't forget to add dependency on filterFilter to you controller.

Please see demo below:


var app = angular.module('app', []);

app.controller('homeCtrl', function($scope, filterFilter) {

  $scope.test = function(feed) {

    if (filterFilter($scope.pickedFeeds, {
      id: feed.id
    }).length > 0) {

      return true;

    }

  }

  $scope.pickedFeeds = [{
    "id": "1",
    "name": "ALLEN Production Lake",
    "status": "IN_PROGRESS",
    "owner": "ih546q"
  }, {
    "id": "2",
    "name": "Demo Lake",
    "status": "IN_PROGRESS",
    "owner": "ih546q"
  }]



  $scope.feeds = [{
    id: 1,
    name: 'ALLEN Production Lake'
  }, {
    id: 2,
    name: 'Demo Lake'
  }, {
    id: 3,
    name: 'mamalake'
  }, {
    id: 4,
    name: 'tatalake'
  }, {
    id: 5,
    name: 'mapalake'
  }, {
    id: 6,
    name: 'gagalake'
  }, {
    id: 7,
    name: 'ramalake'
  }, {
    id: 8,
    name: 'chakalake'
  }, {
    id: 9,
    name: 'dumdumlake'
  }, {
    id: 10,
    name: 'popolake'
  }]


});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">
  <div ng-controller="homeCtrl">

    <div ng-repeat='feed in feeds'>
      <input type="checkbox" id='{{feed.id}}' ng-model="feed.checked" ng-checked="test(feed)" />
      <label for='{{feeds.id}}' class='lake-name'>{{feed.name}}<span class='checkbox-input-style'></span>
      </label>
    </div>
  </div>

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.