"Back button directive Angular" Code Answer's
You're definitely familiar with the best coding language Whatever that developers use to develop their projects and they get all their queries like "Back button directive Angular" answered properly. Developers are finding an appropriate answer about Back button directive Angular related to the Whatever coding language. By visiting this online portal developers get answers concerning Whatever codes question like Back button directive Angular. Enter your desired code related query in the search bar and get every piece of information about Whatever code related question on Back button directive Angular.
Back button directive Angular
/** Navigation service ***/
import { Injectable } from '@angular/core'
import { Location } from '@angular/common'
import { Router, NavigationEnd } from '@angular/router'
@Injectable({ providedIn: 'root' })
export class NavigationService {
private history: string[] = []
constructor(private router: Router, private location: Location) {
this.router.events.subscribe((event) => {
if (event instanceof NavigationEnd) {
this.history.push(event.urlAfterRedirects)
}
})
}
back(): void {
this.history.pop()
if (this.history.length > 0) {
this.location.back()
} else {
this.router.navigateByUrl('/')
}
}
}
/** create directive ****/
import { Directive, HostListener } from '@angular/core'
import { NavigationService } from './navigation.service'
@Directive({
selector: '[backButton]',
})
export class BackButtonDirective {
constructor(private navigation: NavigationService) {}
@HostListener('click')
onClick(): void {
this.navigation.back()
}
}
/*** use below code to use directive **/
<button backButton>Back with NavigationService</button>
Source: nils-mehlhorn.de
All those coders who are working on the Whatever based application and are stuck on Back button directive Angular can get a collection of related answers to their query. Programmers need to enter their query on Back button directive Angular related to Whatever code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about Back button directive Angular for the programmers working on Whatever code while coding their module. Coders are also allowed to rectify already present answers of Back button directive Angular while working on the Whatever language code. Developers can add up suggestions if they deem fit any other answer relating to "Back button directive Angular". Visit this developer's friendly online web community, CodeProZone, and get your queries like Back button directive Angular resolved professionally and stay updated to the latest Whatever updates.