I have a form in angular (v2.4.8) which uses ngFor in a radio selection based on the example in scotch.io. This doesn't work in my application, if I click on a radio the user.category does not get updated.
Code for compoonent:
export class RegistrationComponent implements OnInit {
public user: User;
public userTypes = [
{value: 'type1', display: 'Type 1'},
{value: 'type2', display: 'Type 2'},
{value: 'type3', display: 'Type 3'}
];
constructor() { }
ngOnInit() {
this.user = {
name: '',
email: '',
category: this.userTypes[0].value
}
}
public save(isValid: boolean, f: User) {
console.log(f);
}
}
And the template:
<div class="btn-group" data-toggle="buttons">
<label *ngFor="let userType of userTypes" class="btn btn-outline-secondary">
<input type="radio" name="category" [(ngModel)]="user.category" [value]="userType.value"> {{userType.display}}
</label>
</div>
The user interface is as follows:
export interface User {
name: string;
email: string;
category: string;
}
Edit
This appears to be an issue with using bootstrap btn-group with angular, looks like the click event i handled correctly. I fixed it by adding a click event to the label. Answer below.
*ngForon alabel?