4

I am not getting how to change the button text in ionic2 using ElementRef & Renderer2 class.

Here is what I have tried so far.

@Component({
  selector: 'component',
  templateUrl: `<button #button type="submit" class="form-button" ion-button 
                [disabled]="!form.valid">Change this text</button>`;
})
export class component {
   @ViewChild('button', {read: ElementRef}) 
   private button : ElementRef;

   constructor(private renderer: Renderer2){
   }

   ionViewDidLoad() {
      this.renderer.setProperty(this.button.nativeElement, 'value', 'Cute alligator');
   }
}

This setProperty only change the value of the button which is as attribute. But I want to change the button text which is now Change this text inside button.

Please help. Thanks.

2
  • It is required to use renderer2? Commented Oct 24, 2017 at 13:51
  • you can normally bind property and change it. Commented Apr 18, 2019 at 7:46

2 Answers 2

6

Using Renderer

this.renderer.setProperty(this.button.nativeElement, 'textContent', 'Your Text') 
Sign up to request clarification or add additional context in comments.

1 Comment

This should be marked as correct answer because the question ask 'using renderer2'
2

Just like this:

this.button.nativeElement.innerText = 'your text';

2 Comments

Thanks @vivek-doshi. Its Works.
Please correct your answer in favor of Nishant Singh

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.