YouTube2Text

HTTP in Angular - Learning Angular (Part 8) — Transcript

by Angular · 1,766 words · 282 segments · language en · Watch on YouTube

Full transcript

  1. 0:02welcome back friends and by this point
  2. 0:05if you've been able to complete the
  3. 0:07previous modules you've built a
  4. 0:09beautiful functional application but
  5. 0:11since you've come to this module that
  6. 0:14tells me that you want more and we can
  7. 0:16totally do that right now the
  8. 0:19application retrieves data from a
  9. 0:21hard-coded array in the housing service
  10. 0:24now to make our app a little bit more
  11. 0:27realistic we can add support to make
  12. 0:30HTTP requests for the housing locations
  13. 0:33now the first change that we'll make is
  14. 0:36moving the data from a hard-coded array
  15. 0:38in our application into a standalone
  16. 0:41server on our local machine that we can
  17. 0:44use to make HTTP requests
  18. 0:47from the command line run npm install
  19. 0:50Dash G
  20. 0:52json-server next we need to create a new
  21. 0:55file in the homes Dash app directory
  22. 0:58called db.json once it's been created
  23. 1:01open it in your editor in db.json open
  24. 1:05the file and add the following code
  25. 1:07create an empty Json object then add a
  26. 1:11property wrapped in quotes called
  27. 1:13locations set the value to be an empty
  28. 1:16array
  29. 1:17now we need entries for this array so
  30. 1:20we'll copy the entries from our housing
  31. 1:22service in
  32. 1:24housing.service.ts copy the array
  33. 1:27entries from the housing location list
  34. 1:29class property and paste them into the
  35. 1:31locations array in db.json now to
  36. 1:35confirm in db.json you should have a
  37. 1:38Json object with a property called
  38. 1:40locations that has an array value
  39. 1:42containing the data entries for the
  40. 1:45housing locations now let's confirm that
  41. 1:48our data is working by starting the Json
  42. 1:50server here's how from a command line
  43. 1:53run the following command
  44. 1:55json-server Dash Dash
  45. 1:58watchdb.json when the server starts you
  46. 2:02will receive a URL on my machine it is
  47. 2:06localhost Port 3000 forward slash
  48. 2:09locations opening this URL in the
  49. 2:12browser will reveal our data being
  50. 2:14served to us from the local server that
  51. 2:17we've just created hey that's nice work
  52. 2:19now let's update the application to use
  53. 2:21our new server the first change we'll
  54. 2:23make is to remove the data from the
  55. 2:26service and
  56. 2:27housing.location.ts we're going to
  57. 2:29remove the housing location list
  58. 2:31property and delete the array of values
  59. 2:34as well now we're going to add a
  60. 2:37property called URL and set it to the
  61. 2:40string value
  62. 2:41HTTP colon forward slash forward slash
  63. 2:44localhost support 3000 for slash
  64. 2:47locations in the get all housing
  65. 2:50locations function we need to make a
  66. 2:53call to the API endpoint to retrieve
  67. 2:55this data here's how we'll accomplish
  68. 2:57that first update the method signature
  69. 3:00for get all housing locations to return
  70. 3:02a typed promise the type is housing
  71. 3:06location array also Mark the function as
  72. 3:08async we're going to use the fet API to
  73. 3:12request data from the local Json server
  74. 3:15in the body of get all housing locations
  75. 3:18function remove the existing code
  76. 3:21add a constant local variable called
  77. 3:23data and set the value to a weight Fetch
  78. 3:27and pass in this dot URL as an argument
  79. 3:31to fetch
  80. 3:32we're using the async await pattern here
  81. 3:35to reduce the amount of code that we
  82. 3:37have to write and increase the clarity
  83. 3:39next add a return statement return
  84. 3:43awaits data.json open close parentheses
  85. 3:46because this is a function call then
  86. 3:48nullus coalescing operator empty array
  87. 3:52if for some reason we get a nullest
  88. 3:54value from the call to data.json we'll
  89. 3:57default to returning an empty array
  90. 3:59there's a few things to note here we're
  91. 4:02wrapping the fetch call in a service
  92. 4:04which could seem like adding some
  93. 4:07complexity here in this application we
  94. 4:09only make this call once but in other
  95. 4:12applications we may reuse the same call
  96. 4:15in multiple places also this gives us
  97. 4:18the added benefit of testability we can
  98. 4:21provide a mock implementation of the
  99. 4:22surface in our test that can return some
  100. 4:25data
  101. 4:27to keep things simple we're using the
  102. 4:29browser fetch API for more complex
  103. 4:32examples angular provides the HTTP
  104. 4:35client service class so you can use that
  105. 4:37and to find out more head over to
  106. 4:40angular.io okay great that function has
  107. 4:43been taken care of we're going to repeat
  108. 4:45a lot of these steps for the get housing
  109. 4:48location by ID function
  110. 4:50first let's update the method signature
  111. 4:53to return a typed promise in this case
  112. 4:55though it will be housing location type
  113. 4:59in the body of the function at a
  114. 5:02constant local variable called data set
  115. 5:05to the value await Fetch and as a
  116. 5:08parameter to fetch we're going to use
  117. 5:10JavaScript string interpolation to
  118. 5:13construct our URL
  119. 5:14using backticks interpolate this dot URL
  120. 5:18followed by a forward slash then
  121. 5:21interpolate ID which is the parameter to
  122. 5:24get housing locations by ID
  123. 5:27next add a return statement return await
  124. 5:31data.json open close parentheses then
  125. 5:35nullus coalescing operator empty object
  126. 5:39literal at this point we've changed the
  127. 5:42implementation of the servers but now we
  128. 5:44have to update the calls to this service
  129. 5:47from the other parts of the application
  130. 5:49and
  131. 5:51home.component.ts we're going to update
  132. 5:53the Constructor method to call the new
  133. 5:55promise based service
  134. 5:57remove the existing code in the body of
  135. 6:00the function and add a call to get all
  136. 6:03housing locations
  137. 6:05next attach a then function to the call
  138. 6:09for the parameter to the then function
  139. 6:11provide an arrow function
  140. 6:14the parameter of the function is housing
  141. 6:17location list of type housing location
  142. 6:19array and then the body of the function
  143. 6:22assign this dot housing location list
  144. 6:25the value housing location list
  145. 6:29this is the parameter that we created
  146. 6:32finally we'll update
  147. 6:36details.component.ts in the Constructor
  148. 6:39method we'll replace the existing call
  149. 6:41to get housing location by ID with a
  150. 6:44call to our new promise based function
  151. 6:46delete the code that assigns a value to
  152. 6:50this dot housing location and add the
  153. 6:52following
  154. 6:53make a call to get housing location by
  155. 6:56ID in the housing service and pass in
  156. 6:59the housing location ID local variable
  157. 7:02next attach a then function as a
  158. 7:05parameter to the then function provide
  159. 7:07an arrow function that has a single
  160. 7:09parameter called housing location of
  161. 7:11type housing location in the body of the
  162. 7:14function assign this dot housing
  163. 7:17location the value housing location save
  164. 7:20all your code
  165. 7:22now in the browser we can confirm that
  166. 7:24our housing locations are being
  167. 7:26displayed we can also confirm that the
  168. 7:29application successfully allows users to
  169. 7:32select their housing location and be
  170. 7:34navigated to a populated Details page
  171. 7:38now we're almost done here but there's
  172. 7:40one more feature I'd like to implement
  173. 7:44on the landing page there's a search bar
  174. 7:47and a button that we haven't addressed
  175. 7:49let's turn that into a functioning
  176. 7:52filter to do that we'll need to work
  177. 7:54with a form and a click Handler
  178. 7:59openhome.component.ts we need to
  179. 8:01retrieve the data from the filter in the
  180. 8:03template for this component we could use
  181. 8:06a form group and type forms but that
  182. 8:09might be more than we need for this task
  183. 8:12instead we'll use something called a
  184. 8:14template variable this will allow us to
  185. 8:17get a reference to an element in the
  186. 8:19template and use it in our application
  187. 8:22update the input element and add an
  188. 8:25attribute of the format hashtag filter
  189. 8:28the hashtag syntax creates our template
  190. 8:31variable in this example the variable is
  191. 8:34called filter
  192. 8:36let's update the button element and add
  193. 8:38a click Handler
  194. 8:40add click surrounded by parentheses and
  195. 8:43set it equal to the string value filter
  196. 8:46results and since this is going to be a
  197. 8:49function add parentheses to the end and
  198. 8:52pass in
  199. 8:53filter.value as a parameter one quick
  200. 8:56note you may be wondering where
  201. 8:58filter.value came from well since we are
  202. 9:01using a template variable we have access
  203. 9:03to the actual HTML element value is a
  204. 9:07property on the input HTML element
  205. 9:10in our click Handler invocation we're
  206. 9:12passing in the text value from the input
  207. 9:15directly into our component class let's
  208. 9:18also update the app dash housing Dash
  209. 9:20location in G4 code
  210. 9:23right now we're displaying the housing
  211. 9:25location list but we're going to switch
  212. 9:27things up housing location list will be
  213. 9:30our source of Truth in the application
  214. 9:32for listing so we're going to change a
  215. 9:36housing location list to filtered
  216. 9:38location list in the ng4
  217. 9:42now we'll update the component class add
  218. 9:45a new property called filtered location
  219. 9:47list that's all camel case of type
  220. 9:50housing location array and assign it the
  221. 9:52value of empty array and the Constructor
  222. 9:55in the then function add a new
  223. 9:57expression this dot filter location list
  224. 10:00equals housing location list next we'll
  225. 10:03update the filter results function
  226. 10:07first we'll add code to check for blank
  227. 10:09text if
  228. 10:11not text this dot filtered location list
  229. 10:15equals this.housing location list this
  230. 10:19allows users to clear the search box and
  231. 10:22receive all housing locations
  232. 10:24next this is where we'll handle the
  233. 10:27actual filtering this dot filter
  234. 10:30location list equals
  235. 10:31this.housinglocationlist.filter
  236. 10:35now the array filter method takes a
  237. 10:38callback that defines the criteria so
  238. 10:40provide an arrow function with the
  239. 10:42parameter housing location and the body
  240. 10:45of the function to be housing location
  241. 10:48dot name dot two lowercase parentheses
  242. 10:52because we want to ensure that the
  243. 10:54census case doesn't interfere with our
  244. 10:55comparisons continuing add a call to the
  245. 10:59includes function and provide text Dot
  246. 11:02to lowercase parentheses as an argument
  247. 11:05to the includes function
  248. 11:07save this code and return to the browser
  249. 11:10in the browser input any text and then
  250. 11:13click the search button the results
  251. 11:15update this is huge I am so so proud of
  252. 11:18you and the progress that you've made so
  253. 11:21far so here's what's even cooler right
  254. 11:24now we're searching or filtering on the
  255. 11:26name of the housing location but you
  256. 11:29could expand the filter to compare any
  257. 11:31fields that you want all right friends
  258. 11:33we've covered quite a bit so let's recap
  259. 11:36we've used fetch to make an HTTP request
  260. 11:39to a live endpoint we've refactor our
  261. 11:42service and service calls to use a new
  262. 11:45format
  263. 11:46and we've added filtering capabilities
  264. 11:48to our application with template
  265. 11:51variables and some typescript
  266. 11:53all right way to go you should be very
  267. 11:57excited for your success and I hope that
  268. 11:59you are enjoying the progress that
  269. 12:01you've made and at this point you have
  270. 12:04everything you need to get started on
  271. 12:05building the next great application in
  272. 12:08angular congratulations friends
  273. 12:10now there is so much more to learn but
  274. 12:14you are well on your way I hope that you
  275. 12:17have enjoyed this program as I've
  276. 12:19enjoyed building this application with
  277. 12:21you and traveling with you on your
  278. 12:23angular Learning Journey
  279. 12:26okay friends until the next time
  280. 12:28go build great apps
  281. 12:33foreign
  282. 12:35[Music]

About this transcript

This page contains the full transcript of HTTP in Angular - Learning Angular (Part 8) by Angular, generated from the public captions YouTube serves with the video. The transcript has 1,766 words across 282 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.

What you can do with it

Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.

Free YouTube transcript tool

YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.