0

I am trying to get id's of selected buttons in an array from button-checkbox. I am able to successfully show the selected button's id in view but unable to get the selected ids in array form in controller.

Here is my HTML code

    <html ng-app="plunker">

<head>
  <meta charset="utf-8" />
  <title>AngularJS BootStrap UI radios</title>

  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.js"></script>
  <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.11.2.js"></script>
  <script src="script.js"></script>
  <link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
</head>

<body ng-controller="MainCtrl">
  <div class="btn-group-justified">
    <label ng-repeat="service in availability.services" class="btn btn-primary"  ng-model="service.selected" ng-click="test(availability.services)" btn-checkbox >{{service.name}}</label>
  </div>
  <div ng-repeat="service in availability.services"><span ng-show="service.selected">{{service.id}}</span></div>
</body>

</html>

and here is controller

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

app.controller('MainCtrl', function($scope) {

  var services = [ 
    { name:'Service A',id:1 },
    { name:'Service B',id:2},
    { name:'Service C',id:3 },
    { name:'Service D',id:4}
  ];

  $scope.availability = { services:services };
});

please see the plnker link

http://plnkr.co/edit/DeVPNug9APWWpwZByvnu?p=preview

1
  • 2
    unable to show id in array What do you mean??? Commented Dec 17, 2015 at 12:10

2 Answers 2

1

Here you go:

$scope.getSelectedIDs = function() {
    var ids = [];
    angular.forEach($scope.availability.services, function(service) {
        if (service.selected) {
            ids.push(service.id);
        }
    })

    return ids;
};

Now, just call the method $scope.getSelectedIDs() anywhere in your controller where you want the selected ids in array.

http://plnkr.co/edit/rS47sIEo1HaaDqrc3BUw?p=preview

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

app.controller('MainCtrl', function($scope) {

  var services = [{
    name: 'Service A',
    id: 1
  }, {
    name: 'Service B',
    id: 2
  }, {
    name: 'Service C',
    id: 3
  }, {
    name: 'Service D',
    id: 4
  }];

  $scope.availability = {
    services: services
  };

  $scope.getSelectedIDs = function() {
    var ids = [];
    angular.forEach($scope.availability.services, function(service) {
      if (service.selected) {
        ids.push(service.id);
      }
    })

    return ids;
  };

  $scope.alertMeSelectedIDs = function() {
    console.log($scope.getSelectedIDs());
    alert($scope.getSelectedIDs());
  };
});
<html ng-app="plunker">

<head>
  <meta charset="utf-8" />
  <title>AngularJS BootStrap UI radios</title>

  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.js"></script>
  <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.11.2.js"></script>
  <script src="script.js"></script>
  <link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
</head>

<body ng-controller="MainCtrl">
  <div class="btn-group-justified">
    <label ng-repeat="service in availability.services" class="btn btn-primary" ng-model="service.selected" ng-click="test(availability.services)" btn-checkbox>{{service.name}}</label>
  </div>
  <div ng-repeat="service in availability.services"><span ng-show="service.selected">{{service.id}}</span>
  </div>
  <br>
  <a ng-click="alertMeSelectedIDs()">Show me selected</a>
</body>

</html>

Sign up to request clarification or add additional context in comments.

6 Comments

it's perfect but I just want to show array in console not in view part hope you got it
Yes Sure. I've updated the answer. Open the browser console, hit the above Run code snippet button, select some service and hit Show me selected. You will see the values in console.
Hi @shashank Thanks for your code but please tell me that how I can retrieve that array after click on button I want that dynamic generated array (button are those 4 not through any external link)
I've already added that in the answer yesterday. See alertMeSelectedIDs() method and the <a> tag in HTML.
but I want to show after click on button not through any external link
|
0

I have forked the code: http://plnkr.co/edit/eQxsq194qqRUo8Vs326H?p=preview

app.controller('MainCtrl', function($scope) {

  var services = [ 
    { name:'Service A',id:1 },
    { name:'Service B',id:2},
    { name:'Service C',id:3 },
    { name:'Service D',id:4}
  ];

  $scope.availability = { services:services };


  $scope.test = function (service){
    console.log (service);
  }
});

HTML

<body ng-controller="MainCtrl">
  <div class="btn-group-justified">
    <label ng-repeat="service in availability.services" class="btn btn-primary"  ng-model="service.selected" ng-click="test(service)" btn-checkbox >{{service.name}}</label>
  </div>
  <div ng-repeat="service in availability.services"><span ng-show="service.selected">{{service.id}}</span></div>
</body>

1 Comment

What does this add exactly?

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.