Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Ii can get input value within ionic 2 in my Component?

I will create a shopping app using ionic 2. In the products details I have created a stepper for increment and decrement value of quantity.

How can I get the input value within ionic 2 in my Component?

like image 500
Mahmoud Ismail Avatar asked Jul 17 '16 21:07

Mahmoud Ismail


People also ask

How do you input text value in ionic?

whan you need to access on the value of the input, check the value of inputValue. Two-way binding is a combination of both property binding and event binding as it is a continuous synchronization of data/values from presentation layer to component and from component to the presentation layer.

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.

How do you make ion input readonly?

Expect the 'readonly' attribute to be rendered on the input tag when "variable" is set to true. Steps to reproduce: Add an ion input tag with conditional readonly <ion-input [readonly]="isReadonly"> Set isReadonly variable to true.


2 Answers

Solution:

1- app/pages/index.html

In Angular 2, you should use ngModel in the template.

<ion-header>
  <ion-navbar primary>
  </ion-navbar>
</ion-header>

<ion-content>
  <ion-item>
    <button lightgray full (click)="incrementQty()">+</button>
    <ion-item>
      <ion-input type="number" min="1" [value]="qty" [(ngModel)]="qty"></ion-input>
    </ion-item>
    <button lightgray full (click)="decrementQty()">-</button>
  </ion-item>
</ion-content>

2- app/pages/index.ts

import { Component} from '@angular/core';
import { NavController, Slides} from 'ionic-angular';

@Component({
  templateUrl: 'build/pages/titlepage/titlepage.html',
})
export class titlePage {
  qty:any;
  constructor(private nav: NavController) {
    this.qty = 1;
  }

  // increment product qty
  incrementQty() {
    console.log(this.qty+1);
    this.qty += 1;
  }

  // decrement product qty
  decrementQty() {
    if(this.qty-1 < 1 ){
      this.qty = 1
      console.log('1->'+this.qty);
    }else{
      this.qty -= 1;
      console.log('2->'+this.qty);
    }
  }
}
like image 120
Mahmoud Ismail Avatar answered Oct 07 '22 17:10

Mahmoud Ismail


Or as an alternative solution you may use the more appropriate Form controls designed for angular 2. (learn more here )

Example:

Typescript

import {Component, Input} from '@angular/core';
import {FORM_DIRECTIVES, FormBuilder, ControlGroup, AbstractControl} from '@angular/common';
import {IONIC_DIRECTIVES} from 'ionic-angular';

@Component({
    selector: 'chat-form',
    templateUrl: '/client/components/chat-form/chat-form.html',
    directives: [IONIC_DIRECTIVES, FORM_DIRECTIVES],
    pipes: [],
    styleUrls: []
})
export class ChatFormComponent {
    
    chatForm:ControlGroup;
    
    messageInput:AbstractControl;

    constructor(private translate:TranslateService, private formBuilder:FormBuilder) {
        this.chatForm = formBuilder.group({messageInput: ['']})
        this.messageInput = this.chatForm.controls['messageInput'];
    }

    sendMessage() {
        console.log('sendMessage: ', this.messageInput.value);
    }
}

Template

<form [ngFormModel]="chatForm" (ngSubmit)="sendMessage()">
    <ion-input type="text" [ngFormControl]="messageInput" placeholder="{{'chat.form.placeholder' | translate }}"></ion-input>
    <button fab>
        <ion-icon name="send"></ion-icon>
    </button>
</form>
like image 34
Matyas Avatar answered Oct 07 '22 17:10

Matyas