30.05.2017 Views

Web_Designer_Issue_262_2017

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Developer tutorials<br />

What’s new in Angular 4?<br />

Top<br />

Using the AoT Angular compiler we can see errors before we ship our<br />

app to the browser. In this example we’ve forgotten to assert type for<br />

our ‘item’ parameter<br />

Above<br />

Our email validator easily recognises only valid email addresses and will<br />

only enable the Submit button when one is entered into the form<br />

Right<br />

Titlecase pipe applied to flower description text forces the first letter of<br />

each word to be capitalised. Unfortunately this has also been applied to<br />

the possessive ‘s’ on Friend’S!<br />

}<br />

cancelItem(item: Item) {<br />

item.inBasket = false;<br />

}<br />

15. Email validator<br />

Another handy new feature in 4.0 is the new email<br />

validator, which makes it much easier to validate an<br />

ngForm input type of ‘email’. Maybe our Flower Shop<br />

needs a newsletter. First, import the FormsModule<br />

to ngModule.<br />

import { FormsModule } from '@angular/forms';<br />

@NgModule({<br />

imports: [<br />

[...]<br />

FormsModule<br />

],<br />

16. Form template<br />

Next, add an ngForm field in the item.component<br />

.html template.<br />

<br />

<br />

Enter your email address to subscribe to<br />

our newsletter:<br />

<br />

<br />

<br />

<br />

17. Handle @Input<br />

Include handling for the email address input form field<br />

to item.component.ts.<br />

@Input() emailEntry: string;<br />

handleSubmit(form: any)<br />

{<br />

alert("Email address: "+form.value.<br />

emailEntry+" has been subscribed to the<br />

Flower Shop newsletter");<br />

}<br />

18. Test in the browser<br />

We should find that we can only use the Submit<br />

button when we’ve entered a string that meets<br />

the basic criteria for an email address. Well that<br />

was easy!<br />

19. Structural directive syntax<br />

The final thing we’re going to update is our structural<br />

directive syntax. ngFor and ngIf both have extended<br />

syntax options. ngIf now allows an ‘else’ conditional.<br />

Let’s modify our ngIf in items.component.html to<br />

include an ‘else’. The element selected by the ‘else’<br />

condition must be within an ng-template.<br />

<br />

Item Reserved<br />

Cancel Purchase<br />

<br />

<br />

Buy Flower<br />

<br />

20. ngFor<br />

We have updated syntax in ngFor iterators to enable the<br />

‘as’ keyword to track an index. It’s trivial and doesn’t add<br />

much value to our Flower Shop app, but say we were<br />

receiving high volumes of item.json objects over HTTP<br />

and wanted to limit or count how many we are displaying<br />

on the page, this change makes that much easier.<br />

section class="card" *ngFor="let item of<br />

items; index as i"><br />

{{i}}<br />

<br />

<br />

[...]<br />

<br />

<br />

[...]<br />

<br />

<br />

<br />

21. New pipes<br />

Finally, we have a new pipe! Titlecase can be used to<br />

force capitalisation of the first letter of each word in a<br />

string. This can be useful for consuming data from name<br />

and address form fields, for example. Let’s test it out on<br />

the interpolated item descriptions. There’s lots more<br />

to explore with Angular 4, with the most significant<br />

improvements in larger apps, but hopefully this tutorial<br />

has made updating seem a little less daunting!<br />

<br />

{{item.name}}<br />

{{item.<br />

about|titlecase}}<br />

<br />

84

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!