Keeping in mind that I have a ng-repeat, how do I get only the items I marked in the checkbox?
Keeping in mind that I have a ng-repeat, how do I get only the items I marked in the checkbox?
Here's an example, see if it suits you.
function MyCtrl($scope) {
$scope.itens = [
{id: 1,name: "nome 1"},
{id: 2,name: "nome 2"},
{id: 3,name: "nome 3"}
];
$scope.marcados = function() {
var checkedItems = [];
angular.forEach($scope.itens, function(item, arrayIndex) {
if (item.id === true) {
checkedItems.push(item)
}
})
return checkedItems
}
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script></script><divng-app="" ng-controller="MyCtrl">
<ul>
<li ng-repeat="item in itens">
<input type="checkbox" ng-model="item.id">{{item.name}}
</li>
</ul>
{{itens}}
<br><br>
marcados: {{marcados()}}
</div>