2

I have this htmlbutton:

<button ion-button class="button icon ion-home" round (click)="getColor()" id="one">0</button>

I need to get the value('0' in this case) of this button when it is clicked.

here is my angular code:

import { Component } from '@angular/core';

import { NavController } from 'ionic-angular';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  constructor(public navCtrl: NavController) {

  }
  getColor() { 
    document.getElementById("one").nodeValue="0";

  }

}

I really tried for many hours to get this right but I couldn't. I'm new to angular. Someone please help me with this thing?

4
  • Why you cannot simply pass the 0 to getColor() click handler? Commented Jan 14, 2018 at 21:20
  • How do I do that? @RobYed Commented Jan 14, 2018 at 21:27
  • 1
    (click)="getColor()" will simple be (click)="getColor(0)". Of course, your getColor method then has to expect a parameter. The parameter's value is 0 in your case. Commented Jan 14, 2018 at 21:54
  • I edit my answer, see if it's solves the issue now Commented Jan 16, 2018 at 8:40

3 Answers 3

1

In Html

<button ion-button class="button icon ion-home" round (click)="getColor()" id="one">{{value}}</button>

In Angular

export class HomePage {
  value: number = 0;
  constructor(public navCtrl: NavController) {

  }
  getColor() { 
    console.log(this.value);
  }
}

First define variable in Component then use it in html. This way you will be able to access it easily.

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

2 Comments

Can I use button value and set it to input ?
you can use [(ngModel)]="inputValue" attribute in input. Then in getColor set this.value = this.inputValue; Also don't forget to import ngModel
0

Use the $event property which will contain information about the clicked element, presumably $event.target:

In your HTML:

(click)="getColor($event)"

In your TS file:

getColor ($event: any) {
    // debug the value of $event
}

Or use the method @RobYed provided by passing "0" as the argument.

Comments

0

/*********

**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;
      }

 }

6 Comments

What is ViewChild? Please provide a complete and clean answer
and it says cannot read property 'text content' of undefined
Hellooooo???? Stil I'm getting this error. cannot read property 'text content' of undefined
did you used it on ionViewDidLoad() ? you can access it only after the view as been load
i edited the answer, setting the input value to the button text
|

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.