Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

926
Visualizações
How do I change the text of a button when clicked, in Angular

I want to change the text when this.isDisabled is set to false or vice versa, when the button is clicked. I have also used this.btn.value, but it threw me an error.

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

@Component({
  selector: 'app-root',
  template: `
  <div style="display:flex;align-items:center;flex-direction:column;justify-self:center">
    <h2>Welcome to {{name}} </h2>

    <input [disabled]="isDisabled" [id]="myId" type="text" value="Vishwas"> 

    <button id="changeText" (click)="enableDisable()">Edit</button>
  </div>
  `,
  styles: []
})


export class AppComponent {
  name = 'Something';
  public myId = "testId";
  public isDisabled = true;
  public btn = document.getElementById('changeText');
  

  enableDisable(){
    if(this.isDisabled){
      this.isDisabled=false;
      this.btn.textContent="Save";
    }
    else{
      this.isDisabled=true;
      this.btn.textContent="Edit";
    }
    console.log(this.isDisabled);
  }
}

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

codeSolution:https://stackblitz.com/edit/angular-ivy-or2wqk?file=src%2Fapp%2Fapp.component.html

<button id="changeText" (click)="enableDisable()">
  <span *ngIf="isDisabled; else prompt">
    changeText
  </span>
</button>
<ng-template #prompt>
  Save
</ng-template>

  import { Component} from '@angular/core';
    @Component({
      selector: 'my-app',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    export class AppComponent {
      isDisabled: boolean = false;
    
      enableDisable() {
        this.isDisabled = this.isDisabled ? false : true;
      }
    }

check if this works for you

Explanation

  • on click of button enableDisable() function is called where we toggle isDisabled flag
  • content of the button is changed conditionally on isDisabled flag
  • if isDisabled is true "changeText" else "Save" will display (used Angular Ngif directive for this you can refer https://ultimatecourses.com/blog/angular-ngif-else-then)
over 4 years ago · Santiago Trujillo Relatório

0

I know the accepted answer works, but it's very overcomplicated. This is much simpler.

<button (click)="isDisabled = !isDisabled">
    {{ isDisabled ? 'Edit' : 'Save' }}
</button>
export class AppComponent {
    isDisabled: boolean = false;
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda