16.07.2017 Views

AngularJS Essentials

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Data Handling<br />

Now, let's introduce a new parameter to give the users a chance to change the<br />

plate's separator:<br />

filters.js<br />

parking.filter("plate", function() {<br />

return function(input, separator) {<br />

var firstPart = input.substring(0,3);<br />

var secondPart = input.substring(3);<br />

return firstPart + separator + secondPart;<br />

};<br />

});<br />

Form validation<br />

Almost every application has forms. It allows the users to type data that will be sent<br />

and processed at the backend. <strong>AngularJS</strong> provides a complete infrastructure to easily<br />

create forms with the validation support.<br />

The form will always be synchronized to its model with the two-way data binding<br />

mechanism, through the ngModel directive; therefore, the code is not required to<br />

fulfill this purpose.<br />

Creating our first form<br />

Now, it's time to create our first form in the car parking application. Until now, we<br />

have been using the plate of the car in any format in order to allow parking. From<br />

now on, the driver must mention the details of the plate following some rules. This<br />

way, it's easier to keep everything under control inside the parking lot.<br />

The HTML language has an element called form that surrounds the fields in order to<br />

pass them to the server. It also creates a boundary, isolating the form as a single and<br />

unique context.<br />

With <strong>AngularJS</strong>, we will do almost the same thing. First, we need to surround<br />

our fields with the form element and also give a name to it. Without the name, it<br />

won't be possible to refer to it in the future. Also, it's important to assign a name<br />

to each field.<br />

In the following code, we have added the form to our parking application:<br />

index.html<br />

<br />

[ 62 ]<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!