Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

917
Views
¿Cómo cambio el texto de un botón cuando se hace clic en Angular?

Quiero cambiar el texto cuando this.isDisabled se establece en falso o viceversa, cuando se hace clic en el botón. También he usado this.btn.value , pero me arrojó un 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 answers
Answer question

0

solución de código : 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; } }

comprueba si esto te funciona

Explicación

  • al hacer clic en el botón, se llama a la función enableDisable() donde alternamos el indicador isDisabled
  • el contenido del botón se cambia condicionalmente en el indicador isDisabled
  • si isDisabled es verdadero, "cambiar texto", de lo contrario, se mostrará "Guardar" (usó la directiva Angular Ngif para esto, puede consultar https://ultimatecourses.com/blog/angular-ngif-else-then )
over 4 years ago · Santiago Trujillo Report

0

Sé que la respuesta aceptada funciona, pero es muy complicada. Esto es mucho más simple.

 <button (click)="isDisabled = !isDisabled"> {{ isDisabled ? 'Edit' : 'Save' }} </button>
 export class AppComponent { isDisabled: boolean = false; }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!