banner



How Do I Had Register Button After Register Success In Angular 5

  • Updated date Jul xvi, 2020
  • 51.1k
  • 7

In this commodity you lot will learn how to show/Hibernate Dialog or modal service in Athwart.

Introduction

In this article nosotros will encounter how to create a modal window or dialog box in Athwart. We will use Angular and typescript to prove or hibernate the modal window.

We volition exist using NgbModal in lodge to open the modal. This is just required to create a component and laissez passer the template in its open method.

This also have a Dismiss method for endmost the modal with the particular reason.

Let's starting time creating the application with the dialog service provided on a specified upshot.

We volition modify the existing application to add the Dialog functionality.

Where to apply the modal dialog?

  1. Nosotros will endeavour to evidence the dialog if User is clicking on "Register" button without filling the fields.
  2. We will try to prove the dialog if the User that you lot are trying to register is already present.
  3. We will show dialog if the user is successfully created.

Nosotros volition be using RXJS Subject in order to accomplish the dialog service. Subject is a special type of observable that allow values to multicast to many observers.

Create a modal.service.ts in _services directory and add the below content:

  1. import  { Injectable } from '@angular/core' ;
  2. import  { NgbModal, ModalDismissReasons } from '@ng-bootstrap/ng-bootstrap' ;
  3. @Injectable({
  4.   providedIn:'root'
  5. })
  6. export class  ModalService {
  7.     closeResult ='' ;
  8.     constructor(private  modalService: NgbModal){}
  9.     open(content) {
  10. return this .modalService.open up(content, {ariaLabelledBy: 'modal-basic-championship' })
  11.         .issue.then((issue) => {
  12. return this .closeResult = `Airtight with : ${issue}`;
  13.       }, (reason) => {
  14. return this .closeResult = `Dismissed ${ this .getDismissReason(reason)}`;
  15.       });
  16.     }
  17. private  getDismissReason(reason: whatsoever): string {
  18. if  (reason === ModalDismissReasons.ESC) {
  19. return 'past pressing ESC' ;
  20.       }else if  (reason === ModalDismissReasons.BACKDROP_CLICK) {
  21. return 'by clicking on a backdrop' ;
  22.       }else  {
  23. render  ` with : ${reason}`;
  24.       }
  25.     }
  26. }

Create dialog.service.ts in _services directory and add the below contents:

  1. import  { Injectable } from '@angular/core' ;
  2. import  { Observable, Subject } from 'rxjs' ;
  3. @Injectable({ providedIn:'root'  })
  4. export class  DialogService {
  5. private  dialogSubject = new  Subject<any>();
  6.     sendDialog(dialog: {type: number, message: cord}) {
  7. this .dialogSubject.next(dialog);
  8.     }
  9.     clearDialog() {
  10. this .dialogSubject.next();
  11.     }
  12.     getDialog(): Observable<any> {
  13. return this .dialogSubject.asObservable();
  14.     }
  15. }
  16. Create folio.service.tsin  _services and add beneath contents:
  17. import  { Injectable } from '@angular/cadre' ;
  18. import  { Observable, Subject } from 'rxjs' ;
  19. @Injectable({ providedIn:'root'  })
  20. consign grade  PageService {
  21. individual  pageSubject = new  Subject<any>();
  22.     sendPage(dialog: {type: number, message: string}) {
  23. this .pageSubject.adjacent(dialog);
  24.     }
  25.     clearPage() {
  26. this .pageSubject.next();
  27.     }
  28.     getPage(): Observable<any> {
  29. return this .pageSubject.asObservable();
  30.     }
  31. }

Let's create a component that will have the templates for opening the dialog content.

Create _component directory in app directory.

Create component prompt within _component directory.

Open prompt.component.ts

  1. import  { Component, OnInit, Input, ViewChild, ElementRef } from '@athwart/core' ;
  2. import  { ModalService } from 'src/app/_services/modal.service' ;
  3. import  { DialogService } from 'src/app/_services/dialog.service' ;
  4. import  { Subscription } from 'rxjs' ;
  5. import  { PageService } from 'src/app/_services/page.service' ;
  6. @Component({
  7.   selector:'app-prompt' ,
  8.   templateUrl:'./prompt.component.html' ,
  9.   styleUrls: ['./prompt.component.css' ]
  10. })
  11. export course  PromptComponent implements  OnInit {
  12.   @Input() result: number;
  13.   subscription: Subscription;
  14.   @ViewChild('dialog_required' ) dialog_required: ElementRef;
  15.   @ViewChild('dialog_serverError' ) dialog_serverError: ElementRef;
  16.   @ViewChild('dialog_success' ) dialog_success: ElementRef;
  17.   closeResult ='' ;
  18.   serverError ='' ;
  19.   serverResponse ='' ;
  20.   constructor(individual  modalService: ModalService, individual  dialogService: DialogService, private  pageService: PageService) {
  21. this .outcome = 0;
  22. this .subscription = this .dialogService.getDialog().subscribe(dialog => {
  23. if  (dialog) {
  24. this .open(dialog);
  25.       }
  26.     });
  27.   }
  28.   open(dialogType) {
  29. switch (dialogType.type)
  30.     {
  31. case  one:
  32. this .modalService.open( this .dialog_required).then(information=>{
  33. this .closeResult = information;
  34.           });
  35. suspension ;
  36. instance  2:
  37. this .serverError = dialogType.bulletin;
  38. this .modalService.open( this .dialog_serverError).then(data=>{
  39. this .closeResult = data;
  40.           });
  41. pause ;
  42. example  iii:
  43. this .serverResponse = dialogType.message;
  44. this .modalService.open up( this .dialog_success).and then(data=>{
  45. this .closeResult = information;
  46. this .pageService.sendPage({blazon: one, message: "page" })
  47.           });
  48. break ;
  49. default :
  50.           console.log('no modal opens' );
  51.     }
  52.   }
  53.   ngOnInit():void  {
  54.     console.log(this .upshot)
  55.   }
  56.   ngOnDestroy() {
  57. this .subscription.unsubscribe();
  58.   }
  59. }

Open up prompt.comonent.html and add together the below contents:

  1. <ng-template #dialog_required permit-modal>
  2.   <divform = "carte du jour-header"  style= "background-colour:cyan;" >
  3.     <h4>
  4.       Mistake
  5.     <button blazon="push" class = "close"  aria-label= "Close"  (click)= "modal.dismiss('Cantankerous click')" >
  6.       <span aria-subconscious="true" >×</span>
  7.     </button>
  8.     </h4>
  9.   </div>
  10.   <divclass = "modal-trunk"  manner= "background-color:lightsteelblue" >
  11.     <label>Required fields must be filled!</label>
  12.   </div>
  13. </ng-template>
  14.   <ng-template #dialog_serverError permit-modal>
  15.     <divgrade = "card-header"  fashion= "background-color:cyan;" >
  16.       <h4>
  17.         Error
  18.       <push blazon="push" class = "close"  aria-label= "Close"  (click)= "modal.dismiss('Cross click')" >
  19.         <span aria-hidden="true" >×</span>
  20.       </button>
  21.       </h4>
  22.     </div>
  23.     <divcourse = "modal-body"  style= "background-colour:lightsteelblue" >
  24.       <characterization>{{serverError}}</label>
  25.     </div>
  26.   </ng-template>
  27.   <ng-template #dialog_success allow-modal>
  28.     <divgrade = "modal-header"  style= "background-color:cyan;" >
  29.       <h4class = "modal-title"  id= "modal-basic-title" >Success</h4>
  30.       <button type="push button" class = "close"  aria-label= "Close"  (click)= "modal.dismiss('Cross click')" >
  31.         <bridge aria-hidden="true" >×</span>
  32.       </push button>
  33.     </div>
  34.     <divcourse = "modal-body"  fashion= "groundwork-colour:lightsteelblue" >
  35.       <form>
  36.         <divclass = "form-grouping" >
  37.           <characterizationfor = "serverResponse" >{{serverResponse}}</label>
  38.         </div>
  39.       </form>
  40.     </div>
  41.     <divclass = "modal-footer"  style= "background-color:lightsteelblue" >
  42.       <button blazon="button" class = "btn btn-outline-dark"  (click)= "modal.shut('Save click')" >Ok</button>
  43.     </div>
  44.   </ng-template>

Open Register.component.ts and add the below contents:

  1. import  { Component, OnInit } from '@athwart/core' ;
  2. import  { AuthService } from '../_services/auth.service' ;
  3. import  { User } from '../_model/user' ;
  4. import  { Router } from '@angular/router' ;
  5. import  { DialogService } from '../_services/dialog.service' ;
  6. import  { Subscription } from 'rxjs' ;
  7. import  { PageService } from '../_services/folio.service' ;
  8. @Component({
  9.   selector:'app-annals' ,
  10.   templateUrl:'./register.component.html' ,
  11.   styleUrls: ['./register.component.css' ]
  12. })
  13. export class  RegisterComponent implements  OnInit  {
  14.   component: whatever;
  15.   subscription: Subscription;
  16.   registerResult: number = 0;
  17.   registerUserData =new  User();
  18.   constructor(individual  _authservice: AuthService, individual  router: Router, private  dialogService: DialogService, private  pageService: PageService) {
  19. if  ( this ._authservice.currentUserValue) {
  20. this .router.navigate([ '/paidevents' ]);
  21.     }
  22. this .subscription = this .pageService.getPage().subscribe(page => {
  23. if  (page) {
  24. this .open(page);
  25.       }else  {
  26.       }
  27.     });
  28.   }
  29.   ngOnInit():void  {
  30. this .registerUserData.username
  31.       =this .registerUserData.firstName
  32.       =this .registerUserData.lastName
  33.       =this .registerUserData.password
  34.       =""
  35.   }
  36.   ValidateUser(user){
  37. if (!user.username || !user.firstName || !user.lastName || !user.password) render fake ;
  38. render true ;
  39.   }
  40.   Register(user){
  41. if (! this .ValidateUser(user)) {
  42. this .dialogService.sendDialog({type:ane, message: "Required Fields must be filled.." });
  43. return ;
  44.     }
  45.     console.log(user);
  46. this ._authservice.registerUser(user).subscribe(data=>
  47.       {
  48.         console.log("login Details:" , data);
  49. this .dialogService.sendDialog({type: iii, bulletin: data});
  50.       },
  51.       error => {
  52.         panel.log("Error: " , error);
  53. this .dialogService.sendDialog({type: ii, bulletin: error});
  54.       },
  55.       ()=>{
  56.         console.log("login process complete!" );
  57.       });
  58.     console.log('Logged in!' );
  59.   }
  60.  open up(page){
  61. switch (page.type)
  62.    {
  63. case  1:
  64. this .router.navigate([ '/login' ], { queryParams: { }});
  65. interruption ;
  66. default :
  67.         console.log('some information here..' );
  68.    }
  69.  }
  70. }

Open register.component.html and add the beneath selector at the terminate.

<app-prompt></app-prompt>

Run the application,

Show Hide Dialog Or Modal Service In Angular

Click on Register button.

Show Hide Dialog Or Modal Service In Angular

You will get the dialog with mistake message. Click on close icon.

Let's register a user with proper details. Click on Register push button.

Show Hide Dialog Or Modal Service In Angular

On Successful registration of User you will go the prompt.

Allow'due south create a user that is already registered.

Show Hide Dialog Or Modal Service In Angular

You will get an error indicating the already taken username bulletin.

How it works,

  1. Click on Register push button.
  2. If fields are blank so information technology will sendDialog with type i, message: "Required Fields must be filled..".
  3. From dialog service the prompt component will receive the value of sendDialog and open the dialog accordingly.
  1. On successful creation of User it volition prompt the success dialog.
  2. On clicking on Ok of dialog it will ship the page with type and message through pageService.
  3. On Registering component the pageService is subscribed and on the next immediate folio value it will try to open the page based on parameter passed.
  4. In simple terms, on successful cosmos of User it will evidence you a success prompt and when clicking on Ok push it will redirect to the Login Page.
  5. This is all possible because of Subject nosotros have used in subscription and on the every new folio or dialog value it will notify or emit the consequence to the subscribed observer and make these things possible.

Thank you.

Source: https://www.c-sharpcorner.com/article/show-hide-dialog-or-modal-service-in-angular/

Posted by: kingdion1943.blogspot.com

Related Posts

0 Response to "How Do I Had Register Button After Register Success In Angular 5"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel