Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
10
rated 0 times [  17] [ 7]  / answers: 1 / hits: 5419  / 4 Years ago, wed, june 24, 2020, 12:00:00

I'm using angular material 9.2.4


im working on the angular material mat radio button with an input field, every payment methods will have their own input field. if clicked 'Cash' will show one input field and hide other's input field.


how to show the input field based on the mat radio button selection?


enter





My Code


     <mat-radio-group class="text-left">
<div class="form-group">
<mat-radio-button class="ft-12" *ngFor="let item of itemsList" value="{{item.name}}" (change)="onItemChange(item)">
{{item.name}}
<input type="text" class="form-control" placeholder="0">
</mat-radio-button>
</div>
</mat-radio-group>
```

More From » angular

 Answers
8

Here you go, have a look..


in your component.ts file


Lets say this is your itemsList


itemsList = [
{name: 'Cash'},
{name: 'cheque'},
{name: 'Credit Card'},
{name: 'Cash Voucher'},
];

isVisible = -1; // if you want to show by default some input keep it 0 for first radio, 1 for second and so on. I have kept it -1 so no input is shown by default

onItemChange(item, i) {
console.log({item, i});
this.isVisible = i;
}

In your component.html file


<mat-radio-group class="text-left">
<div class="form-group">
<mat-radio-button class="ft-12" *ngFor="let item of itemsList; let i = index;" value="{{item.name}}" (change)="onItemChange(item, i)">
{{item.name}}
<input type="text" *ngIf="isVisible == i" class="form-control" placeholder="0">
</mat-radio-button>
</div>
</mat-radio-group>

Here is your result


radio 1


enter


Radio 2


enter


Hope it will solve your issue.


[#3384] Monday, June 22, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kaceyr

Total Points: 510
Total Questions: 97
Total Answers: 116

Location: Solomon Islands
Member since Fri, Oct 8, 2021
3 Years ago
kaceyr questions
;