1

I am not able to set onhashchange property using angular 4, i tried to set onhashchange in the following life cycle of the angular 4 but it is not working.

ngAfterContentInit

  ngAfterContentInit() {
    //disabling browser back button
    window.location.hash = "lienholder-details";
    window.location.hash = "Again-lienholder-details";//again because google chrome don't insert first hash into history
    window.onhashchange = function () { window.location.hash = "lienholder-details"; }
  }

ngOnInit

ngOnInit(){
 //disabling browser back button
        window.location.hash = "lienholder-details";
        window.location.hash = "Again-lienholder-details";//again because google chrome don't insert first hash into history
        window.onhashchange = function () { window.location.hash = "lienholder-details"; }
}

how to set the onhashchange property in angular 4

Lijin Durairaj
  • 3,341
  • 11
  • 31
  • 50

2 Answers2

2
@HostListener('window:hashchange', ['$event'])
hashChangeHandler(e) {
  window.location.hash = "lienholder-details";
}

or if you are using the router

constructor(router:Router) {
  router.events.forEach((event) => {
    if(event instanceof NavigationStart) {
    }
    // NavigationEnd
    // NavigationCancel
    // NavigationError
    // RoutesRecognized
  });
}

(see also https://stackoverflow.com/a/38080657/217408)

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
0

I used ngAfterViewChecked event and i am able to set onhashchange property

Lijin Durairaj
  • 3,341
  • 11
  • 31
  • 50