How to get only selected checkboxes in angularjs?

后端 未结 2 1020
自闭症患者
自闭症患者 2020-12-16 03:35

I have ng-repeated data, and am trying to get only the ones the user has selected. I\'m not sure how to do it though, this is what I have:

HTML:

2条回答
  •  無奈伤痛
    2020-12-16 04:23

    JavaScript

    var app = angular.module('plunker', ['ui']);
    
    app.controller('MyCtrl', function($scope) {
        $scope.records = [ { "Id": 1 }, { "Id": 2 }, { "Id": 3 } ];
        $scope.selected = {};
        $scope.ShowSelected = function() {
          $scope.records = $.grep($scope.records, function( record ) {
            return $scope.selected[ record.Id ];
          });
        };      
    });
    

    HTML

    • {{record.Id}}
    Show Selected

    http://plnkr.co/edit/lqtDQ6

    You can store the flag in separately and use it to filter the data.

    updated plunk as some CDN links were broken.

提交回复
热议问题