For this exercise, you may work alone or with another student in this course.
Imagine you are writing an Angular app for a coffee shop (or any shop of your choice) that allows a user to select a drink (or anything your shop offers). You will implement a simple form and incorporate Angular's features including data binding, directive, and input validation. A sample interface for the Awesome Shop is shown below.
You may modify your interface the way you like. Get creative. Feel free to add additional elements you feel should be included and have fun!
To deploy and test your program, please refer to Angular deployment.
ng-form
ng serve
http://localhost:4200
<link>
<link>
.
title
property to include the shop's name
(you decide on the name of your shop).
drinks = ['coffee', 'tea', 'juice'];
<option *ngFor="let drink of drinks">{{ drink }}</option>
FormsModule
into your app.
import { FormsModule } from '@angular/forms';Include
FormsModule
in the @NgModule
's imports
array.
<form>
tag
and assign an ngForm
directive to it.
<form #yourFormTemplateReference="ngForm">
ngModel
to the form inputs
to bind data from the form to view (and also verify the data).
export class Order { constructor( public name: string, public email: string, public phone: number, public drink: string, public tempPreference: string, public sendText: boolean, ){} }
import { Order } from './order';
[ ]
to bind property of
an ngModel
of the form controls. For example, for a name input box,
[ngModel]="orderModel.name"
ngModel
of a name input box as
[(ngModel)]="orderModel.name"
ng-touched
— true when the form control has been visitedng-untouched
— false when the form control has been visitedng-dirty
— true when the form control's value has changedng-pristine
— false when the form control's value has changedng-valid
— true when the form control's value is valid (w.r.t. some rules)ng-invalid
— false when the form control's value has invalid (w.r.t. some rules)
Each of these classes has associated property (with a boolean value):
touched
, untouched
,
dirty
, pristine
,
valid
, invalid
pattern="^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$"
Remember to shutdown the server when you are done
You may modify your interface the way you like. Get creative. Feel free to add additional elements you feel should be included and have fun!
Released under the
CC-BY-NC-SA 4.0 license.