1 import { Directive, Host, HostListener, Input, OnInit, Optional } from '@angular/core';
3 import { NgbNav, NgbNavChangeEvent } from '@ng-bootstrap/ng-bootstrap';
6 selector: '[cdStatefulTab]'
8 export class StatefulTabDirective implements OnInit {
10 cdStatefulTab: string;
12 private localStorage = window.localStorage;
14 constructor(@Optional() @Host() private nav: NgbNav) {}
17 // Is an activate tab identifier stored in the local storage?
18 const activeId = this.localStorage.getItem(`tabset_${this.cdStatefulTab}`);
20 this.nav.select(activeId);
24 @HostListener('navChange', ['$event'])
25 onNavChange(event: NgbNavChangeEvent) {
26 // Store the current active tab identifier in the local storage.
27 if (this.cdStatefulTab && event.nextId) {
28 this.localStorage.setItem(`tabset_${this.cdStatefulTab}`, event.nextId);