File

src/app/modules/questionnaire/guard/can-deactivate.guard.ts

Index

Properties
Methods

Constructor

constructor(resourceService: ResourceService)
Parameters :
Name Type Optional
resourceService ResourceService No

Methods

canDeactivate
canDeactivate(component: ComponentDeactivate)
Parameters :
Name Type Optional
component ComponentDeactivate No
Returns : boolean
import { HostListener, Injectable, Directive } from '@angular/core';
import { CanDeactivate } from '@angular/router';
import { ResourceService } from '@sunbird/shared';

@Injectable()
export class CanDeactivateGuard
  implements CanDeactivate<ComponentDeactivate> {
  constructor(public resourceService: ResourceService) {}
  canDeactivate(component: ComponentDeactivate): boolean {
    if (!component.canDeactivate()) {
      if (
        confirm(this.resourceService.frmelmnts.lbl.confirmBackClick)
      ) {
        return true;
      } else {
        return false;
      }
    }
    return true;
  }
}

@Directive()
export abstract class ComponentDeactivate {
  abstract canDeactivate(): boolean;

  @HostListener('window:beforeunload', ['$event'])
  unloadNotification($event: any) {
    if (!this.canDeactivate()) {
      $event.returnValue = true;
    }
  }
}

results matching ""

    No results matching ""