0

i have a question about DOM replacing with angular. When i click on my button i passed $event. My objective is replace the button childrens with another div with text.

Can you help me please?

<button 
  type="button" 
  class="btn LG_btn-grey animated" 
  (click)="onSubmitResendEmail(user.email, $event);sharedService.getCurrentURL()">
  <app-icons-renderer 
    [name]="'sendEmail'">
  </app-icons-renderer>
</button>

2 Answers 2

2

You can give your button a Template Variable. Then in your Class, access it using @ViewChild. Then use Renderer2 to add children to it.

Here's an example of a Button that adds new Content inside it using this.

Component Class

import { Component, ViewChild, Renderer2, ElementRef } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {

  @ViewChild('myButton') myButton: ElementRef;

  constructor(private renderer: Renderer2) {}

  clickHandler() {
    const div = this.renderer.createElement('div');
    const text = this.renderer.createText('Some text in div');
    this.renderer.appendChild(div, text);
    this.renderer.appendChild(this.myButton.nativeElement, div);
  }

}

Template

<button 
  #myButton
  type="button" 
  class="btn LG_btn-grey animated" 
  (click)="clickHandler()">
  <p>Some content</p>
</button>

Alternative:

If you have some HTML in the form of a string that you want to set as the content of the button,

import { Component, ViewChild, Renderer2, ElementRef } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {

  @ViewChild('myButton') myButton: ElementRef;

  constructor(
    private renderer: Renderer2
  ) {}

  newContent = `<div>Some text in div</div>`;

  clickHandler() {
    this.renderer.setProperty(this.myButton.nativeElement, 'innerHTML', this.newContent);
  }

}

Sample StackBlitz.

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

4 Comments

Hello i have try already your solution, But my element is on a ngFor while. When i try to access the button with ViewChild i only can acces to the first button :(
I don't think you mentioned that in your question. Use @ViewChildren in that case.
You'll have to use a DumbDirective to get all the references of the buttons and do the needful with it.
I may be misreading something, but in your original solution, doesn't it never replace the content, but rather just keeps appending children to the button element?
0

Why don't you have flag value that will tell you which element is visible, something like:

<button>
  <app-icons-renderer [style.visibility]="isEmailSent ? 'hidden' : 'visible'" [name]="'sendEmail'">
  </app-icons-renderer>
  <app-icons-renderer [style.visibility]="isEmailSent ? 'visible' : 'hidden'" [name]="'sentEmail'">
  </app-icons-renderer>
</button>

You can change isEmailSent value with your Button click event.

Update: Not sure if this will keep space occupied, better option could be to use

<app-icons-renderer *ngIf="isEmailSent"
<app-icons-renderer *ngIf="!isEmailSent"

1 Comment

Hello, i need to replace the app-icons-renderer element with a text after some state in a ajax request. So i need to acces my element directly in the ts file.

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.