1 00:00:00,330 --> 00:00:06,720 In this lecture, we are going to look at a common problem with navigating to the same page with a different 2 00:00:06,720 --> 00:00:07,770 route parameter. 3 00:00:08,189 --> 00:00:12,660 We were able to successfully create a single route for multiple clips. 4 00:00:12,960 --> 00:00:19,620 We can identify clips by the value of the route parameter in the URL at the bottom of the page. 5 00:00:19,650 --> 00:00:23,190 We have links to other clips in a future lecture. 6 00:00:23,220 --> 00:00:25,710 We will work on the logic for this section. 7 00:00:26,010 --> 00:00:28,590 However, let's create some dummy links. 8 00:00:28,830 --> 00:00:33,270 It's going to highlight a big problem with our component in your editor. 9 00:00:33,450 --> 00:00:35,670 Open the component template file. 10 00:00:38,170 --> 00:00:42,340 The list of videos can be found under a comment that says video list. 11 00:00:42,700 --> 00:00:45,310 There are a total of three anchor elements. 12 00:00:45,490 --> 00:00:52,090 We're going to focus our attention on the first anchor element of the fifth attribute to the router 13 00:00:52,090 --> 00:00:53,050 linked DirecTV. 14 00:00:55,580 --> 00:01:00,650 The URL will be the following value slash clip slash be. 15 00:01:03,210 --> 00:01:05,430 Let's refresh the page in the browser. 16 00:01:07,900 --> 00:01:12,040 At the moment, the path in my address bar points to clip a. 17 00:01:12,340 --> 00:01:18,940 It doesn't matter what the URL is, as long as it's not pointing to clip B, let's click on the first 18 00:01:18,940 --> 00:01:20,530 clip in the video list. 19 00:01:20,800 --> 00:01:24,250 This should take us to be in the address bar. 20 00:01:24,340 --> 00:01:26,650 The URL was correctly updated. 21 00:01:27,070 --> 00:01:30,490 Angular didn't have a problem with updating the URL. 22 00:01:30,940 --> 00:01:33,850 Unfortunately, not everything was updated. 23 00:01:34,150 --> 00:01:36,940 If we look at the title, it hasn't changed. 24 00:01:37,240 --> 00:01:38,860 Nothing's wrong with angular. 25 00:01:39,100 --> 00:01:41,470 It's the default behavior of the framework. 26 00:01:41,770 --> 00:01:47,890 Whenever we navigate to different pages, Angular will destroy the component of the previous routes. 27 00:01:48,520 --> 00:01:52,360 Angular will notice we're redirecting ourselves to the same page. 28 00:01:52,690 --> 00:01:56,380 It will not destroy a component if it's the same component. 29 00:01:56,710 --> 00:02:00,970 Otherwise, we would be wasting resources and impact performance. 30 00:02:01,270 --> 00:02:05,410 It's not efficient to recreate a component if it's already been loaded. 31 00:02:06,100 --> 00:02:08,500 This behavior works great in our favor. 32 00:02:08,770 --> 00:02:12,010 On the other hand, we need to grab the root parameter. 33 00:02:12,190 --> 00:02:13,840 Let's go back to the editor. 34 00:02:16,380 --> 00:02:23,520 Angular provides two ways to grab information related to a route we can grab information about the route 35 00:02:23,520 --> 00:02:25,380 from the snapshot objects. 36 00:02:25,710 --> 00:02:28,980 Snapshots represent the route at a moment in time. 37 00:02:29,340 --> 00:02:32,640 It does not get updated after we inject the service. 38 00:02:32,970 --> 00:02:38,110 It's great for when we need to grab the router's information when the component is created. 39 00:02:38,760 --> 00:02:44,460 It's not great for when we need to constantly grab the routes information at different moments in time. 40 00:02:44,850 --> 00:02:50,040 Luckily, Angular exposes observables to listen for changes on the routes. 41 00:02:50,340 --> 00:02:57,540 We will replace the code inside the Nji on init function with an observable called this dot route, 42 00:02:57,660 --> 00:02:58,830 not params. 43 00:03:01,400 --> 00:03:08,240 The params property is an observable, this observable will push values whenever the roots parameters 44 00:03:08,240 --> 00:03:09,110 have changed. 45 00:03:09,380 --> 00:03:11,630 Let's subscribe to the observable. 46 00:03:14,190 --> 00:03:19,980 Inside the subscribe, we will pass and an oral function to accept the params object. 47 00:03:22,570 --> 00:03:28,870 The value pushed by the observable will be the same object of parameters from the snapshot object. 48 00:03:29,260 --> 00:03:35,530 Optionally, we can add a tape to this argument if you're interested in type checking at the top of 49 00:03:35,530 --> 00:03:36,220 the file. 50 00:03:36,310 --> 00:03:41,380 We will import the params type from the angular slash route package. 51 00:03:43,880 --> 00:03:47,930 Next, we will annotate the params argument with this type. 52 00:03:50,560 --> 00:03:56,620 Inside this function, we will set the ID property to the Params Scott ID property. 53 00:03:59,230 --> 00:04:05,920 By subscribing to the params observable, we should be able to update the ID property of our component. 54 00:04:06,220 --> 00:04:08,320 Let's test our app one more time. 55 00:04:08,620 --> 00:04:15,040 When you're loading the upload page, update the path in the URL to a different clip other than be. 56 00:04:17,630 --> 00:04:21,019 Next, click on the clip in the more clips section. 57 00:04:21,350 --> 00:04:23,840 This time, the title will be updated. 58 00:04:24,200 --> 00:04:25,310 Mission accomplished. 59 00:04:25,490 --> 00:04:30,080 We were able to update our component whenever the user navigates to a different clip. 60 00:04:30,470 --> 00:04:34,910 This information is going to be helpful once we start uploading files.