ngIf on input value

Viewed 9712

I have field which get the input of the user and according to it the below element will show what has been written

 <label>message</label>
 <input type="text" name="name" [(ngModel)]="person.message"class="form-control">

 <label class="label">you've written this </label>
 <input type="text" name="name" disabled value="{{message}}" class="form-control">

this code works fine but I want to assign a condition to the shown value which is somthing like this: *ngIf=person.message!=null?{{message}}":'write'

which means if the input filed is not null show what is written else show the word write

2 Answers

You can use ternary operator within {{...}}

 <input type="text" name="name" disabled value="{{person.message!=null ? message : 'write'}}" class="form-control">

Or set value as a property with ternary operator.

<input type="text" name="name" disabled [value]="person.message!=null ? message : 'write'" class="form-control">

Just check if the value exist or not else assign the default valus usng OR operator.

<label>message</label>
     <input type="text" name="name" [(ngModel)]="person.message" class="form-control">

     <label class="label">you've written this </label>
     <input type="text" name="name" disabled [value]="person.message ? message : 'write'" class="form-control">
Related