How To Generate Random Password In Angular

Normally, For users, we need to create random password. here we are generate random password using math.random in angular.

At first create new angular application using below command :

ng new random-password

Now, use below code in app.component.ts file :

import { Component } from '@angular/core'

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})

export class AppComponent {

  password: any;

  generatePassword() {
    this.password = Math.random().toString(36).slice(-8);
  }

}

use below code in app.component.html file :

<section class="m-5">
  <h4>How To Generate Random Password</h4>
    <button class="btn btn-primary" type="submit" (click)="generatePassword()">Generate password!</button>
    <p class="mt-2"><strong>Password:</strong> {{ password }}</p>
</section>

Now, run application :

ng serve

output :

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories