Amr Ayoub Amr Ayoub - 1 year ago 143
AngularJS Question

way to get ion-checkbox values

I'm trying to get the value from checkbox so i can bind it .
but i keep get it with the condition ( true or false)

When I click the checkbox, it sets my model like so:

[9: true, 15: true]
What I want is a flat array like:

[9, 15]
Is there a better to get the values from the checkbox and set them in an array on the model?

enter image description here

<ion-checkbox ng-repeat="model in models | filter:query" ng-model="order.models[]" >{{model.model_number}} </ion-checkbox>
current order: {{order}}

.controller('compareModelsCtrl',['$scope','$http', '$state' ,
function($scope, $http, $state,$location) {
$scope.models = data;
$scope.whichmodel = $state.params.modelId;
//$scope.toggleStar = function(model){
// = !;
$scope.order = {};

Answer Source

Ionic framework doesn't do that by default. You need custom function such as format in the code below. Since, I don't have your data.json file, I mimicked two ion-checkbox.

     <ion-content ng-controller="LoginCtrl">
       <ion-checkbox  ng-model="order[0]" ng-change="format()" >1 </ion-checkbox>
       <ion-checkbox  ng-model="order[1]" ng-change="format()" >2</ion-checkbox>
    current order: {{modifiedOrder}}

And implemented following function inside the controller.

angular.module('ionic.example', ['ionic'])
.controller('LoginCtrl', function($scope) {
  angular.forEach($scope.order, function(value, key) {

The order now you had before is modifiedOrder See working code here. Goodluck!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download