Forms in Angular - Learning Angular (Part 7) — Transcript
Full transcript
- 0:02hey friends welcome back so glad that
- 0:04you could join us to continue learning
- 0:06angular in the module on services and
- 0:09dependency injection we updated The
- 0:12Details page to have a button that read
- 0:14applied now okay so that's great but we
- 0:18didn't include any functionality that
- 0:20would allow user to actually apply for a
- 0:23housing location so in this session
- 0:26we'll change that we're going to add an
- 0:29application form and provide users of
- 0:31scaffolding to apply for a housing
- 0:34location so how do we make that happen
- 0:37well we'll use angular forms forms allow
- 0:41users to input data into a web
- 0:43application forms have controls such as
- 0:46buttons check boxes and input fields
- 0:49in angular we can write data models that
- 0:52represent these form controls and give
- 0:54us access to the values in a smooth
- 0:56programmatic way we can take advantage
- 0:59of input validation as well validation
- 1:02allows us to catch bad or malformed data
- 1:05in our application
- 1:06so let's start by building the form
- 1:09model that represents the data that we
- 1:11want to collect in the form so that's
- 1:14user first name user last name and email
- 1:19address
- 1:20and
- 1:21details.component.ts we need to import
- 1:23some classes that help us work with
- 1:26forms import form control form group
- 1:30reactive forms module from at angular 4
- 1:34slash forms the reactive forms module
- 1:37will give us access to some template
- 1:39features for forms so let's update the
- 1:42component decorator metadata Imports
- 1:44property value add another entry to the
- 1:47array for reactive forms module okay
- 1:50great
- 1:51we've configured our component to use
- 1:53forms the next step is to Define that
- 1:55form model that I mentioned let's do
- 1:58that now
- 1:59add a property to the details component
- 2:01class called apply form camel case and
- 2:05set the value equal to a new instance of
- 2:08form group a form group represents a
- 2:11collection of form controls that make up
- 2:14a form in our case we have three data
- 2:17points that will represent as form
- 2:19controls first name last name and email
- 2:23the form group instance accepts an
- 2:25object literal defining the controls for
- 2:28the group
- 2:29update the code to pass in an object
- 2:31literal with the following property name
- 2:33and values property name first name
- 2:36value new instance of form control
- 2:40now the form control accepts a default
- 2:43value we'll set the default value as an
- 2:46empty string
- 2:47next property is last name value new
- 2:52instance of form control with an empty
- 2:54string as a parameter
- 2:57then add a property for email use the
- 3:00value new instance of form control with
- 3:03empty string as a parameter okay
- 3:06excellent we have a form group to
- 3:08represent the data we want to collect
- 3:10and we have access to it in our
- 3:13component class when we bind this form
- 3:15to the template we'll be able to keep
- 3:18the values users enter into the form and
- 3:21sync with the form group this is an
- 3:24enormously valuable convenience that
- 3:26angular provides for us the next step we
- 3:29need to complete is connecting the apply
- 3:32form form group instance to the template
- 3:35and
- 3:37details.component.ts will update the
- 3:39template property of the component
- 3:41decorative metadata
- 3:43the section with the class listing Dash
- 3:46Supply currently has two children
- 3:48elements an H2 and a button delete the
- 3:52button element and replace it with a
- 3:55form element
- 3:57add the form group attribute to the form
- 3:59and bind it to apply form property
- 4:03now don't forget to wrap the form group
- 4:05attribute and square brackets to
- 4:07activate binding next we'll add some
- 4:10labels and inputs to our form add a
- 4:13label element as a child to the form
- 4:15element set the four attribute to equal
- 4:18first Dash name and the text first name
- 4:22as the content of the label element now
- 4:25add a sibling input element set the ID
- 4:28attribute to be first Dash name and the
- 4:31type to be text
- 4:33also include an attribute called form
- 4:36control name camelcase and this is
- 4:38important and set the value equal to
- 4:41first name now let's repeat the same
- 4:44process for last name and email
- 4:47for the last name add a sibling label
- 4:50element and set the four attribute to
- 4:52the last Dash name value then add a
- 4:55sibling input element and set the ID to
- 4:58last stash name the types of text and
- 5:01the form control name to be last name
- 5:03and for email add a sibling label
- 5:06element and set the four attributes to
- 5:08email and add a sibling input element
- 5:10set the ID to email the type to email
- 5:13and set the form control name to email
- 5:17finally we're ready to reintroduce our
- 5:20button add a sibling element for the
- 5:22button set the button type attribute
- 5:24equal to submit
- 5:26and the class to equal primary
- 5:30save this code and check the browser all
- 5:33right excellent we have our updated
- 5:34Details page with the form bound to the
- 5:37data you are doing so great high five
- 5:40next let's configure the application to
- 5:43take action when a user clicks the apply
- 5:45button back in
- 5:47details.component.ts we're going to
- 5:49update the template property for this
- 5:52component add the following attribute to
- 5:55the form element
- 5:57submit but wrap it in parentheses then
- 6:00set the value equal to a function call
- 6:03submit application
- 6:05let's take a moment to clarify what's
- 6:07going on here we've had property binding
- 6:10with square brackets already but in this
- 6:12case we're using a different type of
- 6:15binding and that's event binding submit
- 6:18is an event and when we want to bind to
- 6:20events like click for example we use
- 6:22parentheses as the syntax that signals
- 6:25we're performing event binding right now
- 6:28in the editor submit application will be
- 6:30causing an error because the function is
- 6:32not defined let's create that function
- 6:34now in the body of details component
- 6:36class add a function called submit
- 6:39application it doesn't take any
- 6:41parameters and it doesn't return
- 6:43anything and the body of the function
- 6:46we're going to make a call to a new
- 6:48function in the housing service this dot
- 6:52housing service
- 6:53dot submit application now pass in this
- 6:58dot
- 7:00applyform.value.firstname double
- 7:01question mark empty string this Dot
- 7:06plyform.value.lastname double question
- 7:08mark empty string and this dot
- 7:12applyform.value.email double question
- 7:13mark empty string okay two things to
- 7:17note here before moving on first to
- 7:21access the values from the form group
- 7:23apply form we use the value property and
- 7:26from there we can access the values of
- 7:29the individual form controls
- 7:31now what do those question marks even
- 7:34mean those are the knowledge coalescing
- 7:37operator if the value on the left hand
- 7:40side is null or undefined then the value
- 7:43on the right hand side is used okay back
- 7:47to the implementation details we need to
- 7:49create that function in the server so
- 7:52let's take care of that now
- 7:54and
- 7:55housing.service.ts add a new function to
- 7:58the housing service class called submit
- 8:01application it accepts three parameters
- 8:03of type string first name last name and
- 8:07email
- 8:08in the short term add a console.log to
- 8:12the body of the function and print out
- 8:14the values to the console in the future
- 8:17we can do something much cooler all
- 8:19right save this code and let's head over
- 8:22to the browser now the application has
- 8:25the form and the button submits that
- 8:27form while passing the data to the
- 8:29service now try clicking on the apply
- 8:32button to confirm that the data is being
- 8:34displayed in the browser console you
- 8:37should be really proud of the work that
- 8:39you've done so far this application has
- 8:41grown significantly and we've included
- 8:44many Core Concepts used to build
- 8:46applications in angular
- 8:49okay friends let's recap what we've
- 8:51covered in this session together we've
- 8:54learned about angular forms form groups
- 8:57and form controls we've learned how to
- 8:59bind a form group to an HTML form and
- 9:02how to retrieve the data and pass it to
- 9:04other parts of the application
- 9:07finally we also learned how to use event
- 9:10binding in angular as well again this is
- 9:13really great work and right now with the
- 9:16skills that you have you can get started
- 9:18building web applications with angular
- 9:20and technically you could stock right
- 9:23here but
- 9:24if you'd like to continue we have
- 9:26another section where we'll add even
- 9:28more cool features hope to catch you
- 9:30there
- 9:32foreign
- 9:38[Music]
About this transcript
This page contains the full transcript of Forms in Angular - Learning Angular (Part 7) by Angular, generated from the public captions YouTube serves with the video. The transcript has 1,385 words across 220 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.