Since we just learned about passing data between the HTML and TypeScript files, let’s use that knowledge to make the form for new users to register - which will be on the register page!
We will want the following:
an input field for the user’s email address
a password field
a password confirm field.
We will need variables to store each of those pieces of information, so we need to declare those variables in the global variable declarations portion of the TypeScript file! Open register.component.ts and declare the following 3 variables, right underneath where you declare the class, and right above the constructor().
Now when anyone types in any of the input tags, that variable will be automatically updated with the new value! We aren’t really doing anything with that value yet, so let’s work on that next - doing something with that value. For now, we can just log the user’s email and password results to the console. Later, once we connect our website to Firebase, we can worry about pushing those values to Firebase and actually creating a user account.
When someone presses the “enter” key, or when they press the Submit button we want to run a function. Adjust your HTML code to include the following for all three input tags, as well as the submit button. Note: this is just part of your html from this file, we're just adding in the keyup.enter and click event on the submit button.
Since we’ve told our HTML file to call the registerUserfunction when the user pressed enter or clicks the submit button. We should probably create that registerUser function! Open register.component.ts and create that function now. This is what the code for the whole file should look like:
Serve up your website and check it out. Uh oh, we have an error! Let’s look into the error we’ve received. You should see this error: Can't bind to 'ngModel' since it isn't a known property of 'input'
Well, that looks like the kind of thing that we could Google and find an answer for. This is something that developers do constantly. Run into an error? Check for typos and then run to Google.
Practice time:
Take up to 5 minutes and search Google for that error. Pay attention to results from StackOverflow, and see if you figure out the problem.
Alright, so hopefully you discovered results related to importing FormsModule. Angular has grown very large - so large that it’s incredibly bulky, so the Angular team has split it into multiple parts, and developers only import the parts they need. Not every website needs to have forms, so the Angular team has broken the “forms” part out into its own self-contained module. Let’s import that module now, open app.module.ts and import the FormsModule.
Check out your app now and it should be fixed, with no errors in the console! Now let’s test that registration form we just created. If you fail to complete the form, or your password and passwordConfirm fields don’t match then nothing should happen. If you’ve correctly filled it out, then you should see your answers printed to the console!
Since we’re at the end of a module, let’s do a quick git commit.