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?
- Nosotros will endeavour to evidence the dialog if User is clicking on "Register" button without filling the fields.
- We will try to prove the dialog if the User that you lot are trying to register is already present.
- 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:
- import { Injectable } from '@angular/core' ;
- import { NgbModal, ModalDismissReasons } from '@ng-bootstrap/ng-bootstrap' ;
- @Injectable({
- providedIn:'root'
- })
- export class ModalService {
- closeResult ='' ;
- constructor(private modalService: NgbModal){}
- open(content) {
- return this .modalService.open up(content, {ariaLabelledBy: 'modal-basic-championship' })
- .issue.then((issue) => {
- return this .closeResult = `Airtight with : ${issue}`;
- }, (reason) => {
- return this .closeResult = `Dismissed ${ this .getDismissReason(reason)}`;
- });
- }
- private getDismissReason(reason: whatsoever): string {
- if (reason === ModalDismissReasons.ESC) {
- return 'past pressing ESC' ;
- }else if (reason === ModalDismissReasons.BACKDROP_CLICK) {
- return 'by clicking on a backdrop' ;
- }else {
- render ` with : ${reason}`;
- }
- }
- }
Create dialog.service.ts in _services directory and add the below contents:
- import { Injectable } from '@angular/core' ;
- import { Observable, Subject } from 'rxjs' ;
- @Injectable({ providedIn:'root' })
- export class DialogService {
- private dialogSubject = new Subject<any>();
- sendDialog(dialog: {type: number, message: cord}) {
- this .dialogSubject.next(dialog);
- }
- clearDialog() {
- this .dialogSubject.next();
- }
- getDialog(): Observable<any> {
- return this .dialogSubject.asObservable();
- }
- }
- Create folio.service.tsin _services and add beneath contents:
- import { Injectable } from '@angular/cadre' ;
- import { Observable, Subject } from 'rxjs' ;
- @Injectable({ providedIn:'root' })
- consign grade PageService {
- individual pageSubject = new Subject<any>();
- sendPage(dialog: {type: number, message: string}) {
- this .pageSubject.adjacent(dialog);
- }
- clearPage() {
- this .pageSubject.next();
- }
- getPage(): Observable<any> {
- return this .pageSubject.asObservable();
- }
- }
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
- import { Component, OnInit, Input, ViewChild, ElementRef } from '@athwart/core' ;
- import { ModalService } from 'src/app/_services/modal.service' ;
- import { DialogService } from 'src/app/_services/dialog.service' ;
- import { Subscription } from 'rxjs' ;
- import { PageService } from 'src/app/_services/page.service' ;
- @Component({
- selector:'app-prompt' ,
- templateUrl:'./prompt.component.html' ,
- styleUrls: ['./prompt.component.css' ]
- })
- export course PromptComponent implements OnInit {
- @Input() result: number;
- subscription: Subscription;
- @ViewChild('dialog_required' ) dialog_required: ElementRef;
- @ViewChild('dialog_serverError' ) dialog_serverError: ElementRef;
- @ViewChild('dialog_success' ) dialog_success: ElementRef;
- closeResult ='' ;
- serverError ='' ;
- serverResponse ='' ;
- constructor(individual modalService: ModalService, individual dialogService: DialogService, private pageService: PageService) {
- this .outcome = 0;
- this .subscription = this .dialogService.getDialog().subscribe(dialog => {
- if (dialog) {
- this .open(dialog);
- }
- });
- }
- open(dialogType) {
- switch (dialogType.type)
- {
- case one:
- this .modalService.open( this .dialog_required).then(information=>{
- this .closeResult = information;
- });
- suspension ;
- instance 2:
- this .serverError = dialogType.bulletin;
- this .modalService.open( this .dialog_serverError).then(data=>{
- this .closeResult = data;
- });
- pause ;
- example iii:
- this .serverResponse = dialogType.message;
- this .modalService.open up( this .dialog_success).and then(data=>{
- this .closeResult = information;
- this .pageService.sendPage({blazon: one, message: "page" })
- });
- break ;
- default :
- console.log('no modal opens' );
- }
- }
- ngOnInit():void {
- console.log(this .upshot)
- }
- ngOnDestroy() {
- this .subscription.unsubscribe();
- }
- }
Open up prompt.comonent.html and add together the below contents:
- <ng-template #dialog_required permit-modal>
- <divform = "carte du jour-header" style= "background-colour:cyan;" >
- <h4>
- Mistake
- <button blazon="push" class = "close" aria-label= "Close" (click)= "modal.dismiss('Cantankerous click')" >
- <span aria-subconscious="true" >×</span>
- </button>
- </h4>
- </div>
- <divclass = "modal-trunk" manner= "background-color:lightsteelblue" >
- <label>Required fields must be filled!</label>
- </div>
- </ng-template>
- <ng-template #dialog_serverError permit-modal>
- <divgrade = "card-header" fashion= "background-color:cyan;" >
- <h4>
- Error
- <push blazon="push" class = "close" aria-label= "Close" (click)= "modal.dismiss('Cross click')" >
- <span aria-hidden="true" >×</span>
- </button>
- </h4>
- </div>
- <divcourse = "modal-body" style= "background-colour:lightsteelblue" >
- <characterization>{{serverError}}</label>
- </div>
- </ng-template>
- <ng-template #dialog_success allow-modal>
- <divgrade = "modal-header" style= "background-color:cyan;" >
- <h4class = "modal-title" id= "modal-basic-title" >Success</h4>
- <button type="push button" class = "close" aria-label= "Close" (click)= "modal.dismiss('Cross click')" >
- <bridge aria-hidden="true" >×</span>
- </push button>
- </div>
- <divcourse = "modal-body" fashion= "groundwork-colour:lightsteelblue" >
- <form>
- <divclass = "form-grouping" >
- <characterizationfor = "serverResponse" >{{serverResponse}}</label>
- </div>
- </form>
- </div>
- <divclass = "modal-footer" style= "background-color:lightsteelblue" >
- <button blazon="button" class = "btn btn-outline-dark" (click)= "modal.shut('Save click')" >Ok</button>
- </div>
- </ng-template>
Open Register.component.ts and add the below contents:
- import { Component, OnInit } from '@athwart/core' ;
- import { AuthService } from '../_services/auth.service' ;
- import { User } from '../_model/user' ;
- import { Router } from '@angular/router' ;
- import { DialogService } from '../_services/dialog.service' ;
- import { Subscription } from 'rxjs' ;
- import { PageService } from '../_services/folio.service' ;
- @Component({
- selector:'app-annals' ,
- templateUrl:'./register.component.html' ,
- styleUrls: ['./register.component.css' ]
- })
- export class RegisterComponent implements OnInit {
- component: whatever;
- subscription: Subscription;
- registerResult: number = 0;
- registerUserData =new User();
- constructor(individual _authservice: AuthService, individual router: Router, private dialogService: DialogService, private pageService: PageService) {
- if ( this ._authservice.currentUserValue) {
- this .router.navigate([ '/paidevents' ]);
- }
- this .subscription = this .pageService.getPage().subscribe(page => {
- if (page) {
- this .open(page);
- }else {
- }
- });
- }
- ngOnInit():void {
- this .registerUserData.username
- =this .registerUserData.firstName
- =this .registerUserData.lastName
- =this .registerUserData.password
- =""
- }
- ValidateUser(user){
- if (!user.username || !user.firstName || !user.lastName || !user.password) render fake ;
- render true ;
- }
- Register(user){
- if (! this .ValidateUser(user)) {
- this .dialogService.sendDialog({type:ane, message: "Required Fields must be filled.." });
- return ;
- }
- console.log(user);
- this ._authservice.registerUser(user).subscribe(data=>
- {
- console.log("login Details:" , data);
- this .dialogService.sendDialog({type: iii, bulletin: data});
- },
- error => {
- panel.log("Error: " , error);
- this .dialogService.sendDialog({type: ii, bulletin: error});
- },
- ()=>{
- console.log("login process complete!" );
- });
- console.log('Logged in!' );
- }
- open up(page){
- switch (page.type)
- {
- case 1:
- this .router.navigate([ '/login' ], { queryParams: { }});
- interruption ;
- default :
- console.log('some information here..' );
- }
- }
- }
Open register.component.html and add the beneath selector at the terminate.
<app-prompt></app-prompt>
Run the application,
Click on Register button.
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.
On Successful registration of User you will go the prompt.
Allow'due south create a user that is already registered.
You will get an error indicating the already taken username bulletin.
How it works,
- Click on Register push button.
- If fields are blank so information technology will sendDialog with type i, message: "Required Fields must be filled..".
- From dialog service the prompt component will receive the value of sendDialog and open the dialog accordingly.
- On successful creation of User it volition prompt the success dialog.
- On clicking on Ok of dialog it will ship the page with type and message through pageService.
- 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.
- 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.
- 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
0 Response to "How Do I Had Register Button After Register Success In Angular 5"
Post a Comment