Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How create an input box having a + and - button in Ionic

How I can create an input box having a + and - button. Clicking upon which user can change the quantity of product selected, like this screen:

input box having a + and - button

like image 1000
Ramos Avatar asked Aug 09 '16 21:08

Ramos


People also ask

How do you assign a value to an input field in Ionic?

By using [(ngModel)]="value" you bind the value variable to that input field. That way it is always updated. If you change it in the UI, it is immediately updated "in the code". And when you update it in your code, it automatically updates in the UI.


2 Answers

Here's a quickly thrown together example for Ionic 2. If you are using Ionic 1 you should be able to adapt it pretty easily.

You just need a couple controller/class functions to increment and decrement, then call those on tap from the buttons. This example covers just one button, so something like this wrapped in an ngFor or a <ion-list>

page.ts:

private currentNumber = 0;
constructor () { }

private increment () {
  this.currentNumber++;
}

private decrement () {
  this.currentNumber--;
}

page.html:

<ion-icon name="remove-circle" (click)="decrement()">
{{currentNumber}}
<ion-icon name="add-circle" (click)="increment()">
like image 98
amuramoto Avatar answered Sep 21 '22 07:09

amuramoto


As for ionic v.1 at your template you could have something like:

<div class="flex_row">
  <button class="button icon ion-minus-circled red" ng-click="sub(item)">
  <p> {{item.quantity}} </p>
  <button class="button icon ion-plus-circled green" ng-click="add(item)">
</div>

At your css

    .red:before {
    color: red;
    }

    .green:before {
    color: green;
    }

    .flex_row {
    display: -webkit-flex;
    display: flex;
    -webkit-flex-direction: row; 
    flex-direction: row;
    }

And in your controller

$scope.sub = function(i) {
  i.quantity--;
}

$scope.add = function(i) {
  i.quantity++;
}
like image 35
korteee Avatar answered Sep 19 '22 07:09

korteee