Skip to main content

angular - How to use ternary operator

app.component.ts

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

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


export class AppComponent {
  isPhoneAllowed:boolean = false;
  
  toggleValue(){
    this.isPhoneAllowed = !this.isPhoneAllowed;
  }
}
app.component.html

<h2>Angular Ternary Operator Example</h2>

<h3>
    Phone is 
    {{isPhoneAllowed? "allowed" : "not allwed"}}
</h3>

<button 
    (click)="toggleValue()"
    style="padding:12px;">
        Toggle Value
</button>
More Angular tutorials