3

How can i change the text in the mat-button when i toggle it?

<form class="example-form" style="text-align: center;" [formGroup]="loginForm" (ngSubmit)="buttonClicked()">
  <mat-card [@myAwesomeAnimation]='state' (mouseenter)="animateMe()" (mouseleave)="animateMe()" style="padding: 2vh">
      <input type="text" class="mainInput" style="font-size: 18px;" matInput autofocus shouldLabelFloat="false" placeholder="Insert Your URL Here" autocomplete="off">
  </mat-card>
  <br>
  <button mat-raised-button color="accent"></button>

</form>
1
  • ps: i don't know why but i doesn't work for a form problem
    – Gianmarco
    Commented Dec 15, 2017 at 9:59

2 Answers 2

11

You could have a toggle variable ?

Then use it as follow

<button (click)="doToggle()">{{toggle ? 'Toggled':'Not Toggled'}}</button>

Then in TS

toggle:boolean = false;

doToggle():void{
   this.toggle = !this.toggle;
   // Do some other stuff needed
 }

Please see fiddle

12
  • i have the button in the app component, i have to make an other component?
    – Gianmarco
    Commented Dec 15, 2017 at 8:17
  • @Ladvace Not sure what you are asking, but the example given wont require a new component. just add the variable and method to AppComponent Commented Dec 15, 2017 at 8:19
  • i don't know why he refresh the page when i click on the button
    – Gianmarco
    Commented Dec 15, 2017 at 9:32
  • @Ladvace Could you post a jsfiddle please? Commented Dec 15, 2017 at 11:18
  • @Ladvace I have added a fiddle to test and play with Commented Dec 15, 2017 at 11:31
2

I don't know what toggle means, but if you're looking for text change, then

toggled: boolean = false; // Declare a boolean that holds the toggle value

<!-- Use a ternary condition -->
<button mat-raised-button (click)="toggled = !toggled" color="accent">{{toggled ? 'Toggled' : 'Not toggled'}}</button>
4
  • You are fast :) Commented Dec 15, 2017 at 8:06
  • i means when i click on it
    – Gianmarco
    Commented Dec 15, 2017 at 8:07
  • What if I want the toggled text to be something that I will be sending from the typescript? That is, it will be a custom text, that will change every time. Then? Commented Jul 24, 2018 at 6:33
  • then simply use {{ myTextToChange }} without condition, and change the value of that variablein your typescript code or with (click)="myTextToChange = 'Hello world'" ...
    – user4676340
    Commented Jul 24, 2018 at 6:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.