Angular 7 Templating
You may have noticed that one of the components we generated was called nav. Let's implement a header bar with a navigation in our app!
The first step is to visit the app.component.html file and specify the following contents:
Angular Tutorial: Learn Angular
Templating step by step
<app-nav></app-nav>
<section>
<router-outlet></router-outlet>
</section>
So, we've removed a bunch of templating and placed in <app-nav></app-nav>, what does this do and where does it come from?
Well, if you visit /src/app/nav/nav.component.ts you will see that in the component decorator, there's a selector property bound to the value of app-nav. When you reference the selector of a given component in the form of a custom HTML element, it will nest that component inside of the component it's that's referencing it.
If you save the file you just updated, you will see in the browser we have a simple, nav works! And that's because the nav.component.html file consists of a simple paragraph stating as much.
At this point, let's specify the following HTML to create a simple navigation:
<header>
<div class="container">
<a routerLink="/" class="logo">apptitle</a>
<nav>
<ul>
<li><a routerLink="/">Home</a></li>
<li><a routerLink="/about">About</a></li>
<li><a routerLink="/contact">Contact us</a></li>
</ul>
</nav>
</div>
</header>
The only thing that might look a little strange is routerLink. This is an Angular 7 specific attribute that allows you to direct the browser to different routed components. The standard href element will not work.
While we're here on the subject of templating, what if we wanted to display properties that are coming from our component? We use what's called interpolation.
Make the following adjustment to our template:
<!-- From: -->
<a routerLink="/">myapp</a>
<!-- To: -->
<a routerLink="/">{{ appTitle }}</a>
Interpolation is executed by wrapping the name of a property that's defined in the component between {{ }}.
Let's define that property in nav.component.ts:
export class NavComponent implements OnInit {
appTitle: string = 'myapp';
// OR (either will work)
appTitle = 'myapp';
constructor() { }
ngOnInit() {
}
}
You can use the TypeScript way of defining properties or standard JavaScript. Save the file and you will see myapp is back in the template.
There's a lot more to templating, but we will touch on those topics as we continue. For now, let's apply style to our header.
First, let's visit the global stylesheet by opening /src/styles.scss and define the following rulesets:
@import url('https://fonts.googleapis.com/css?family=Montserrat:400,700');
body, html {
height: 100%;
margin: 0 auto;
}
body {
font-family: 'Montserrat';
font-size: 18px;
}
a {
text-decoration: none;
}
.container {
width: 80%;
margin: 0 auto;
padding: 1.3em;
display: grid;
grid-template-columns: 30% auto;
a {
color: white;
}
}
section {
width: 80%;
margin: 0 auto;
padding: 2em;
}
Visit nav/component.scss and paste the following contents:
header {
background: #7700FF;
.logo {
font-weight: bold;
}
nav {
justify-self: right;
ul {
list-style-type: none;
margin: 0; padding: 0;
li {
float: left;
a {
padding: 1.5em;
text-transform: uppercase;
font-size: .8em;
&:hover {
background: #8E2BFF;
}
}
}
}
}
}
If you save and refresh, this should be the result in the browser:
Comments
Post a Comment