Module 3: Angular Data Binding
Goals
Learn about Angular “data-binding”
Dive further into the TypeScript file
Learn about showing and hiding content with Angular
Pass information between the HTML and TypeScript files
Set up a basic Angular form
JavaScript/TypeScript in HTML the Angular way
Open up header.component.ts
and create a variable called nameToDisplay
, set it equal to any name you like.
Now update the logout button in the header.component.html
file to show the value of our variable.
See those double curly braces? This how you pass data from JavaScript/TypeScripts to html in Angular. It’s a one-way deal, nothing gets sent back to the TypeScript file, and it’s updated in real time. If the value of “nameToDisplay” was to change for any reason, that change would be immediately reflected in the HTML file.
Passing Functions in Angular
If you think back to react, you may remember, running functions onClick
, Angular does things similarly but the syntax is a bit different. Edit your header.component.html
to look like the following:
Angular knows that when it receives a click, to run the function (either called login or logout). So, let's define those methods so it can use them. Add the following in your header.component.ts
file, underneath your ngOnInit
but still inside of your component.
Let's also, have nameToDisplay start out as an empty string. Your whole header.component.ts
file should look like this:
Now, open up your app in the browser, and click login and type your name. Make sure that it correctly displays your name, and that when you click logout it clears your name.
Next, we'll break down the Angular Component.
Last updated