Hi Dev,
This simple article demonstrates of angular material radio button reactive form example. i would like to share with you radio button angular material example. you will learn angular material radio button example. you can understand a concept of angular material radio button reactive form.
I’m going to show you about angular material mat radio button reactive form. if you have question about angular material radio button example then i will give simple example with solution.
We can create material radio button in angular 6, angular 7, angular 8 and angular 9.
I will give you two simple example with angular:
1) Basic Material Radio Button
2) Material Radio Button with Reactive Form
You can see bellow layout for demo:
Create New App
If you are doing example from scratch then You can easily create your angular app using bellow command:
ng new app-material
Add Material Design
Now in this step, we need to just install material design theme in our angular application. so let's add as like bellow:
ng add @angular/material
Cmd like bellow:
Installing packages for tooling via npm.
Installed packages for tooling via npm.
? Choose a prebuilt theme name, or "custom" for a custom theme: Indigo/Pink
[ Preview: https://material.angular.io?theme=indigo-pink ]
? Set up global Angular Material typography styles? Yes
? Set up browser animations for Angular Material? Yes
Example 1: Basic Material Radio Button
Here, we will create very simple example. first we need to import MatRadioModule for mat-radio-button material design. so let's update app.module.ts, app.component.ts and app.component.html.
Let's follow step:
src/app/app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import {MatRadioModule} from '@angular/material/radio';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
BrowserAnimationsModule,
MatRadioModule,
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
src/app/app.component.html
<h4>Angular Radio Button Example</h4>
<mat-radio-group
aria-labelledby="example-radio-group-label"
class="example-radio-group">
<mat-radio-button class="example-radio-button" *ngFor="let food of foods" [value]="food">
{{food}}
</mat-radio-button>
</mat-radio-group>
src/app/app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'app-material3';
foods: string[] = ['Banana', 'Apple', 'Mango'];
}
Example 2: Material Radio Button with Reactive Form
Here, we will create very simple example using reactive form. first we need to import MatRadioModule, MatButtonModule, FormsModule and ReactiveFormsModule for mat-radio-button material design. so let's update app.module.ts, app.component.ts and app.component.html.
Let's follow step:
src/app/app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import {MatRadioModule} from '@angular/material/radio';
import {MatButtonModule} from '@angular/material/button';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
BrowserAnimationsModule,
FormsModule,
ReactiveFormsModule,
MatRadioModule,
MatButtonModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
src/app/app.component.html
<h1>Angular Material Radio Button Example - HDTuto.com</h1>
<form [formGroup]="form" (ngSubmit)="submit()">
<mat-radio-group
aria-labelledby="example-radio-group-label"
class="example-radio-group"
formControlName="foodField">
<mat-radio-button class="example-radio-button" *ngFor="let food of foods" [value]="food">
{{food}}
</mat-radio-button>
</mat-radio-group>
<div *ngIf="form.get('foodField').hasError('required')" class="error">
Food is required
</div>
<button mat-raised-button color="accent">Submit</button>
</form>
src/app/app.component.ts
import { Component } from '@angular/core';
import { FormBuilder, FormGroup, Validators} from '@angular/forms';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'app-material2';
foods: string[] = ['Banana', 'Apple', 'Mango'];
form: FormGroup = new FormGroup({});
constructor(private fb: FormBuilder) {
this.form = fb.group({
foodField: ['', [Validators.required]],
})
}
get f(){
return this.form.controls;
}
submit(){
console.log(this.form.value);
}
}
You can easily run by following command:
ng serve
I hope it can help you...
Do you like below Tutorials ?
- Laravel 5.6 - Collection could not be converted to int
- Laravel - How to generate secure https URL from route?
- Laravel - Vue JS File Upload Example
- How to get last 7 days data in Laravel?
- Laravel Validation required if other field empty example
- Laravel Eloquent - When Case Statement in Select Query Example
- Laravel 7.x and 6.x Passing Variable to Javascript Example
- How to pass PHP variables in JavaScript or jQuery?