2014-12-05 54 views
0

我有對象的簡單數組:如何檢查複選框在陣列ID的媒體鏈接存在於角:

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

然後我有一個列表「飼料」是這樣的:

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' 
      }] 
     }; 
最後的名單上

我對他們有複選框:

<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> 

我要做到以下幾點:

我想檢查[V]那些已準備好拾取數組中的數字, 當我取消選中要移除的數據時,如果存在pickFeeds列表中,等等。 我應該怎麼做?我的ng模型應該是什麼?

+0

可能重複:http://stackoverflow.com/questions/20415704/angularjs-data-binding-checkboxes-to-object - 如果核對 – Christian 2014-12-05 13:10:43

回答

1

您可以創建功能檢查,如果在pickedFeeds存在元素與此相同ID feed.id即:

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

之後,你可以添加到您的視圖:

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

只是不要忘記添加依賴filterFilter給你的控制器。

請參閱下面的演示:


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>