Learn Angular: Angular Navigation Parameters

Pubblicato il: 13 marzo 2020
sul canale di: Brian Lagunas
4,807
100

Want to learn Angular? In this video, "Learn Angular: Angular Navigation Parameters", we learn how to we can start passing navigation parameters in an Angular 9 application.

The first step is to pass the navigation parameters during navigation.

Start by defining a route that will accept a parameter. This is done by specifying a "/:parameName" in the path of the route as shown below.

{ path: "product/:id", component: ProductDetailComponent }

Next, you pass the parameter by using the "routerLink" directive shown below.

[routerLink]="['/product', product.productId]"

In this case, we are binding the parameter value to the "product.productId" property of an object.

Step two is to read the parameter. Start by injecting the ActivatedRoute object into your component's constructor like show below:

constructor(private route: ActivatedRoute)

Now you have two options to read the parameters; Using the snapshot, or using an observable.

Using the Snapshot:
The snapshot property of the ActivatedRoute returns the initial value of the route. You can then access the paramsMap array to access the value of the parameter.

let id = this.route.snapshot.paramMap.get("id");
this.product = this.productService.getProduct(id);

Use the Snapshot option, if you only need the initial value.

Using an Observable:
We usually retrieve the value of a parameter in the ngOninit life cycle hook, when the component initialised. However, when the user navigates to a component again, Angular does not create the new component, but reuses the existing instance. In such circumstances, the ngOnInit method of the component is not called again. Hence you need a way to get the value of the new/updated parameter.

To do this, we will use an observable. By subscribing to the observable paramMap property, you will retrieve the latest value of the parameter and update the component accordingly.

this.sub = this.route.paramMap.subscribe(params =-- {
let id = params.get("id");
this.product = this.productService.getProduct(id);
})
}

Use this option if you expect the value of the parameter to change over time.

"Desktop to Web: A WPF Developers Guide to Learning Angular" is a video tutorial series that will help you take your WPF and WinForms desktop coding skill to the web with Angular. This series will help you understand how your current desktop skills map directly to concepts in Angular to make your learning path to the web as easy and painless as possible.

During each video in this series I will be giving away a one year subscription to Infragistics Ultimate valued at $1,995 USD. Simply subscribe to my channel, like the video, and leave a comment to be entered. Winners are announced in the next video in the series.

Official contest rules: https://brianlagunas.com/desktop-to-w...

The Prize: https://www.infragistics.com/products...

Follow Me:
Twitter:   / brianlagunas  
Twitch:   / brianlagunas  
Blog: http://brianlagunas.com
GitHub: https://github.com/brianlagunas

Sponsor Me:
https://github.com/sponsors/brianlagunas


In questa pagina del sito puoi guardare il video online Learn Angular: Angular Navigation Parameters della durata di ore minuti seconda in buona qualità , che l'utente ha caricato Brian Lagunas 13 marzo 2020, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 4,807 volte e gli è piaciuto 100 spettatori. Buona visione!