Form Radio Button with Angular Material(mat radio button)

Today, We want to share with you Form Radio Button with Angular Material(mat radio button).In this post we will show you Angular 9 Material Radio Button Example | mat radio button in Angular, hear for angular material radio-button example we will give you demo and example for implement.In this post, we will learn about angular material radio-button default selected with an example.

Form Radio Button with Angular Material(mat radio button)

There are the Following The simple About angular material mat-radio-button default checked not working after adding name on mat-radio-group Full Information With Example and source code.

As I will cover this Post with live Working example to develop angular 6 radio button example, so the Angular Bootstrap Radio Button -Bootstrap 4 & Material is used for this example is following below.

Angular is a platform for building mobile & desktop web Based user friendly and light weight applications.Angular,It’s TypeScript-based open-source Angularjs web application framework.

Keywords : angular material radio button reactive form example, radio button angular material example, angular material radio button example, angular material radio button reactive form, radio button angular material example

Make New App & Add Material Design

ng new app-material

ng add @angular/material

Example 1: Basic Material Radio Button

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 - www.pakainfo.com</h4>
  
<mat-radio-group
  aria-labelledby="example-radio-group-label"
  class="example-radio-group">
  <mat-radio-button class="example-radio-button" *ngFor="let product of products" [value]="product">
    {{product}}
  </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';
  
  products: string[] = ['Mobile', 'Iphon', 'Laptop'];
}

Example 2: Material Radio Button with Reactive Form

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 - Pakainfo.com</h1>
   
<form [formGroup]="form" (ngSubmit)="submit()">
  
  <mat-radio-group
    aria-labelledby="example-radio-group-label"
    class="example-radio-group"
    formControlName="productField">
    <mat-radio-button class="example-radio-button" *ngFor="let product of products" [value]="product">
      {{product}}
    </mat-radio-button>
  </mat-radio-group>
  <div *ngIf="form.get('productField').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';
  
  products: string[] = ['Mobile', 'Iphon', 'Laptop'];
  form: FormGroup = new FormGroup({});
  
  constructor(private fb: FormBuilder) {
  
    this.form = fb.group({
      productField: ['', [Validators.required]],
    })
  }
    
  get f(){
    return this.form.controls;
  }
    
  submit(){
    console.log(this.form.value);
  }
}

Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about mat-radio-group example.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1.   Angular 9/8 Radio Button on Form Submit
  2.   Form Select Dropdown with Angular Material(mat-select)
  3.   Angular 9/8 Material Radio Button Examples
  4.   Angular 9/8 Material Textarea Examples
  5.   ng-table using Angular Example
  6.   Angularjs ng-keypress Event Handling Directive
  7.   Simple Angular Hello World Example
  8.   Angular 9/8 Get Checked Radio Button on Change Event
  9.   AngularJS Arrays - tips, tricks and examples
  10.   Angular 6 AJAX Tutorial Example From Scratch