/*********
**EDIT : **
I added the setting of the input value with the button text.
**********/
using ElementRef and ViewChild is one way
ElementRef
- is the most basic abstraction. If you observe it’s class structure, you’ll see that it only holds the native element it’s associated with. It’s useful for accessing native DOM element
//
@ViewChild -
Angular provides a mechanism called DOM queries. It comes in a form of @ViewChild decorator.
You can use ViewChild to get
the first element or the directive matching the selector from the view
DOM.
from the doc ..
basically assing to an element in your html code 'id' (but in the angular way - not id="...") somthing like #someId,
than you can access that element object using
@ViewChild('someId') myElement: ElementRef
can read about it here Exploring Angular DOM manipulation
the code:
<button #btn
ion-button class="button icon ion-home" round
(click)="getColor()">
0
</button>
<ion-input #myInput></ion-input>
// ==================
import { Component, ElementRef, ViewChild } from '@angular/core';
import { NavController, TextInput } from 'ionic-angular';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
@ViewChild('btn') btn: ElementRef;
@ViewChild('myInput') myInput: TextInput;
constructor(public navCtrl: NavController) {}
getColor() {
const btnText = this.btn.nativeElement.textContent;
this.myInput.value = btnText; // here you set the value of the
// input to the text on your button
}
}
// =============================================================//
author approach, send the HTML object as a parameter to the function you call on (click)
<button #btn
ion-button class="button icon ion-home" round
(click)="getColor(btn)">
0
</button>
<ion-input #myInput></ion-input>
// =====================
import { Component, ViewChild } from '@angular/core';
import { NavController, TextInput } from 'ionic-angular';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
@ViewChild('myInput') myInput: TextInput;
constructor(public navCtrl: NavController) {}
getColor(myBtn: HTMLButtonElement) {
this.myInput.value = myBtn.innerHTML;
}
}
// =============================================================//
one more way, maybe the most simplistic is with property binding.
bind the'value' property of ion-input with class member.
<button #btn
ion-button class="button icon ion-home" round
(click)="getColor(btn)">
0
</button>
<ion-input [value]="inputValue"></ion-input>
// =====================
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
inputValue: string = "";
constructor(public navCtrl: NavController) {}
getColor(myBtn: HTMLButtonElement) {
this.inputValue = myBtn.innerHTML;
}
}