YouTube2Text

Services in Angular - Learning Angular (Part 6) — Transcript

by Angular · 2,000 words · 312 segments · language en · Watch on YouTube

Full transcript

  1. 0:02all right friends welcome back we've
  2. 0:05completed the routing and we came across
  3. 0:07a challenge how do we get the selected
  4. 0:10housing location data to be available in
  5. 0:12the details component
  6. 0:14we're going to solve this with a concept
  7. 0:16called a service now a service has many
  8. 0:19different functions or purposes and we
  9. 0:22get to decide which of these directions
  10. 0:24will take in our application one way a
  11. 0:27service can be used is to function as
  12. 0:29the way to interact with the data for
  13. 0:31example instead of each component having
  14. 0:33a copy of the housing location data we
  15. 0:36can create a service that is responsible
  16. 0:38for retrieving the data from the data
  17. 0:40source providing the helpful utility
  18. 0:43functions that allow us to access the
  19. 0:45data and in some cases having functions
  20. 0:48that allow us to persist data to a data
  21. 0:50source so let's start by creating a
  22. 0:53service using the angular CLI and by the
  23. 0:55way have you noticed how helpful the
  24. 0:57angular CLI is yeah I'm just saying it's
  25. 1:00pretty cool so from the command line in
  26. 1:02your project enter the following in g g
  27. 1:06s housing in this case the S is an
  28. 1:09abbreviation for service and we're
  29. 1:12naming it housing
  30. 1:13in the
  31. 1:17ideopenedhousing.service.ts a service is
  32. 1:19a typescript class in angular the
  33. 1:22injectable decorator tells angular that
  34. 1:24we can use this class in the dependency
  35. 1:27injection system meaning that other
  36. 1:29parts of the application can request an
  37. 1:32instance of the service the provided end
  38. 1:35property of the injectable decorated
  39. 1:37metadata tells the angular where in the
  40. 1:40application disk servers can be injected
  41. 1:42now root means that it can be used
  42. 1:44throughout the application we need to
  43. 1:47move the data from the home component to
  44. 1:49this service and
  45. 1:51housing.service.ts let's import the
  46. 1:54housing location interface from dot
  47. 1:57forward slash housing location next add
  48. 2:00a protective property to the housing
  49. 2:03service class called housing location
  50. 2:06list and make that of type housing
  51. 2:09location array for now assign housing
  52. 2:12location list and empty array we need to
  53. 2:14populate the array with data now we'll
  54. 2:17copy the data from the home component so
  55. 2:20in homecomponent.ts copy the contents of
  56. 2:23the housing location array and paste it
  57. 2:26into the housing location list array in
  58. 2:28housing.service.ts
  59. 2:31back in housing.service.cs we're going
  60. 2:34to add two methods to the service
  61. 2:37first add a method named get all housing
  62. 2:40locations that takes no parameters but
  63. 2:43returns a housing location array
  64. 2:46we'll use this method to return the list
  65. 2:49of housing locations to the caller in
  66. 2:52the body of the function return this dot
  67. 2:55housing location list now wherever we
  68. 2:58inject this service we can request the
  69. 3:01housing location list we also don't have
  70. 3:03to worry about duplicated data because
  71. 3:06the servers will now be the source of
  72. 3:08Truth the next method we need to add is
  73. 3:11get housing location by ID which accepts
  74. 3:15a parameter called ID of type number
  75. 3:19and it Returns the union type of housing
  76. 3:23location or undefined we're using the
  77. 3:26typescript union type here because the
  78. 3:29call of this function could pass in an
  79. 3:31ID that doesn't match any housing
  80. 3:32location
  81. 3:34in the body of the function we'll search
  82. 3:36the array here's how this dot
  83. 3:40housinglocationlist dot find and to the
  84. 3:43find function will pass in an arrow
  85. 3:46function as a parameter the arrow
  86. 3:48function has a parameter of housing
  87. 3:51location and the logic is this one-liner
  88. 3:55housing location dot ID triple equals ID
  89. 4:00the find function will return the first
  90. 4:03match in the array so we don't have to
  91. 4:05worry about any unnecessary iterations
  92. 4:08here
  93. 4:09now be sure to save your progress we're
  94. 4:11going to update a few more files to use
  95. 4:13our fancy new service the first
  96. 4:15component will update to use the new
  97. 4:17housing service is
  98. 4:20home.component.ts update the Imports for
  99. 4:23the file to include inject from at
  100. 4:26angular forward slash core and housing
  101. 4:29servers from dot dot forward slash
  102. 4:31housing dot service then in the body of
  103. 4:35the home component add a property called
  104. 4:37housing service make that Camel case and
  105. 4:41set the value to a call to the inject
  106. 4:45function with housing Service as a
  107. 4:47parameter remember that property we had
  108. 4:49called housing location list make sure
  109. 4:52you've removed all of the contents of
  110. 4:54the array so that the value of the
  111. 4:56property is just an empty array
  112. 4:59create a function called Constructor
  113. 5:01that accepts no parameters in the body
  114. 5:04of the function let's add the logic to
  115. 5:06populate the housing location list
  116. 5:08property
  117. 5:10this dot housing location list equals
  118. 5:14this dot
  119. 5:16housingservice.get all housing locations
  120. 5:19and don't forget to add the parentheses
  121. 5:21at the end because we're invoking the
  122. 5:24get all housing locations function now
  123. 5:27save all the code and let's check the
  124. 5:29browser if we navigate to localhost Port
  125. 5:334200 the application will be functioning
  126. 5:36just as it was before we refracted the
  127. 5:39code to use services this is really
  128. 5:42excellent work so far friends
  129. 5:45thumbs up now this is an important
  130. 5:47change that will actually improve the
  131. 5:49app's overall quality let's continue and
  132. 5:52get the detail page functioning properly
  133. 5:55before we go on we should take a quick
  134. 5:58break to discuss why we're using inject
  135. 6:00angular uses a concept called dependency
  136. 6:03injection to allow components and other
  137. 6:06parts of the code to ask the framework
  138. 6:08for things that are needed to function
  139. 6:10also known as dependencies
  140. 6:13at first this might seem like an extra
  141. 6:16step but it gives us some great benefits
  142. 6:19first it allows us to have testable code
  143. 6:22when writing unit tests we can make
  144. 6:24mocks of things like databases and
  145. 6:27online resources without having to call
  146. 6:29them in our test code
  147. 6:31second we can have more reusable code we
  148. 6:36can create our dependencies and use
  149. 6:38dependency injection to get access in
  150. 6:40other parts of the application
  151. 6:43third we benefit from maintainability as
  152. 6:47well when our code is Loosely coupled we
  153. 6:50can change the behind the scenes details
  154. 6:53and update our code over time without
  155. 6:55having to impact other parts of the code
  156. 6:57unnecessarily
  157. 7:00all right friends back to the code in
  158. 7:04details.component.ts let's import
  159. 7:06housing servers from dot dot forward
  160. 7:09slash housing.service we also need to
  161. 7:12import housing location from dot dot
  162. 7:15slash housing location next let's add a
  163. 7:18reference to the service by adding a
  164. 7:20property to the component class called
  165. 7:22housing service where the value is a
  166. 7:25call to the inject function with housing
  167. 7:28Service as a parameter now be sure you
  168. 7:31use your class and not your property as
  169. 7:34the parameter be mindful there we are
  170. 7:37also going to do some slight refactoring
  171. 7:39too so delete the Declaration of housing
  172. 7:42location ID as a Class Property next add
  173. 7:45another class property and that is
  174. 7:47housing location of Union type housing
  175. 7:50location or undefined then in the body
  176. 7:53of the Constructor instead of this dot
  177. 7:56housing location ID update the code to
  178. 7:59const housing location ID we can make
  179. 8:03this ID a local variable now that we
  180. 8:05have access to an actual housing
  181. 8:07location instance finally let's update
  182. 8:10the code to include the following logic
  183. 8:12this dot housing location equals this
  184. 8:16dot housing
  185. 8:18locationservice.get housing location by
  186. 8:20ID and as a parameter to this function
  187. 8:22pass in housing location ID wait
  188. 8:26we're not done making edits because our
  189. 8:28code now has an error in the template to
  190. 8:31resolve that replace the interpolated
  191. 8:33property housing location ID with
  192. 8:36housing location question mark dot ID
  193. 8:40okay so what are we doing here well in
  194. 8:44this case we have to guard against
  195. 8:45calling dot ID on undefined so we use
  196. 8:49the optional chaining operator from
  197. 8:51typescript if the housing location is
  198. 8:53undefined the ID property won't be
  199. 8:55accessed and we don't have to worry
  200. 8:57about an error here save this code and
  201. 9:00let's go back to the browser click on
  202. 9:02learn more for any of the housing
  203. 9:04locations when The Details page renders
  204. 9:07we'll find the ID of the selected
  205. 9:09housing location being displayed all
  206. 9:12right again this is really outstanding
  207. 9:14work friends this application is coming
  208. 9:17along wonderfully and you should be
  209. 9:18really proud of yourselves and the work
  210. 9:20that you've done now we've reached the
  211. 9:22point where we can finally create the
  212. 9:24details component template back in the
  213. 9:27IDE in
  214. 9:29details.component.ts let's update the
  215. 9:32component metadata template property
  216. 9:35delete the existing HTML code here and
  217. 9:37replace it with an article element now
  218. 9:40add an image tag is a child element of
  219. 9:43the article in this case give the image
  220. 9:45element a source attribute of housing
  221. 9:48location question mark dot photo wrap
  222. 9:51the source attribute in square brackets
  223. 9:53to enable property binding because the
  224. 9:55housing location could be undefined
  225. 9:57we'll use the optional chaining operator
  226. 10:00where an error accessing properties in
  227. 10:03this particular template next we're
  228. 10:05going to add the title and location
  229. 10:06we'll start by adding another child to
  230. 10:09the article element a section with the
  231. 10:12class name listing Dash description give
  232. 10:15the section two children an H2 element
  233. 10:19with a class of listing Dash heading and
  234. 10:22then using interpolation at the housing
  235. 10:24location name property as a child of the
  236. 10:27H2 add another child to the section
  237. 10:30element this time a paragraph element
  238. 10:32set the child content to be the
  239. 10:35following interpolate the housing
  240. 10:38location city property then to the right
  241. 10:41of the interpolated value add a comma
  242. 10:43followed by space and then interpolate
  243. 10:46the housing location State Property all
  244. 10:49right now that we set up the name
  245. 10:51location and location image let's add a
  246. 10:55section for some details we're going to
  247. 10:58add another section as a child of the
  248. 11:01article element set the class of the
  249. 11:03section element to be listing Dash
  250. 11:06features
  251. 11:08add an H2 element with the class name
  252. 11:10section Dash heading as a child of the
  253. 11:14list features section
  254. 11:16set the contents of the element to be
  255. 11:18about this location
  256. 11:21next we're going to list the features of
  257. 11:24the location number of units in both
  258. 11:27Wi-Fi and laundry status so let's add
  259. 11:30the following code to the listing Dash
  260. 11:32features section add a UL as a sibling
  261. 11:36to that H2 and give the UL
  262. 11:393li children elements the first child
  263. 11:42has the text units available colon and
  264. 11:46then the interpolated value of housing
  265. 11:48location available units
  266. 11:51the second Ally has a Content does this
  267. 11:54location have Wi-Fi colon followed by
  268. 11:58the interpolated value of housing
  269. 12:00location Wi-Fi and the final Li content
  270. 12:03is does this location have laundry colon
  271. 12:06followed by the interpolated value of
  272. 12:09housing location laundry now we're going
  273. 12:12to add one more panel that will expand
  274. 12:14on in the next section but we'll add the
  275. 12:16placeholder here for now
  276. 12:19as a sibling to the listing feature
  277. 12:21section let's add a new section with the
  278. 12:25class listing Dash apply this section
  279. 12:28has two children the first is an H2 with
  280. 12:32the class section Dash heading with the
  281. 12:35content applied to live here
  282. 12:38and the second element is a button with
  283. 12:40the class primary and the text apply now
  284. 12:44as for the Styles copy the styles from
  285. 12:47the URL Linked In the resources of this
  286. 12:50video and add them to
  287. 12:52details.component.css
  288. 12:55now save all of the code and check the
  289. 12:58browser our Details page now displays
  290. 13:01the details of the selected housing
  291. 13:03location and you know what's great about
  292. 13:04this it works for all the locations
  293. 13:07because the URLs are Dynamic how cool is
  294. 13:11that
  295. 13:12all right friends that's the end of this
  296. 13:15section let's recap what we've covered
  297. 13:17we've learned how to create a service
  298. 13:21how to inject a service into a component
  299. 13:23for use and we've completed our Details
  300. 13:27page Bravo very well done technically at
  301. 13:31this point you have enough skills to get
  302. 13:34started building angular applications
  303. 13:36you should be proud of yourselves I know
  304. 13:39I'm very proud of you
  305. 13:41if you want to continue to expand your
  306. 13:43angular skills then join me in the next
  307. 13:46section where we'll explore even more
  308. 13:48angular features and further build out
  309. 13:50the functionality all right friends
  310. 13:52catch you in the next one
  311. 13:56foreign
  312. 14:01[Music]

About this transcript

This page contains the full transcript of Services in Angular - Learning Angular (Part 6) by Angular, generated from the public captions YouTube serves with the video. The transcript has 2,000 words across 312 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.