Navigation and Login form

Copy from the templates at

  • switch to bootstrap 3.3.7
  • navigate to Getting started
  • click on the Jumbotron example
  • inspect the code
  • select all the code from the tag nav class navabar

Inside the app folder right click and Generate Component and call it nav.

Open nav.component.html and paste the code.

Remove the navabar-fixed-top class from the outer nav.

Change the project name to Dating App.

Inside the div#navbar add a new ul with a few a with a placeholder url.

<ul class="nav navbar-nav">
  <li><a href="#">Matches</a></li>
  <li><a href="#">Lists</a></li>
  <li><a href="#">Messages</a></li>

Open app.component.html, remove some unused code and add the app-nav tag at the top of the page.

  Dating APP

Login form


  • when the user click the submit of the form it will call the login method
  • if the fields are not valorized a simple form validation disables the login button


In nav.component.ts

  • add a model field (without a custom type for now)
  • add a login function
export class NavComponent implements OnInit {
  model: any = {};
  login() {

In nav.component.html add a template reference variable to the form named loginForm.

<form #loginForm="ngForm" class="navbar-form navbar-right">

Import FormModule

We need to configure the app to import the FormsModule to able to use the ngForm directive.

Open app.module.ts and add the import.

import { FormsModule } from '@angular/forms';
imports: [

Now that the directive is imported the form can bind the controls to the model.

Bind form items

Inside each input add (using Angular Snippets a-ngModel) the ngModel two way binding directive.

Add the html5 attributes required and name to both inputs.

Bind the form ngSubmit event to the login function.

Bind the disabled attribute of the button to !loginForm.valid.

The value of loginForm.valid is automatically populated by Angular like the dirty and touched properties.

<form #loginForm="ngForm" class="navbar-form navbar-right" (ngSubmit)="login()">
  <div class="form-group">
    <input type="text" placeholder="Username" class="form-control" required name="username" [(ngModel)]="model.username">
  <div class="form-group">
    <input type="password" placeholder="Password" class="form-control" required name="password" [(ngModel)]="model.password">
  <button type="submit" [disabled]="!loginForm.valid" class="btn btn-success">Sign in</button>