Angular 2 By Example by Chandermani Arora & Kevin Hennessy
Author:Chandermani Arora & Kevin Hennessy
Language: eng
Format: epub, mobi
Publisher: Packt Publishing
Published: 2016-11-14T00:00:00+00:00
Using ngModel with input and textarea
Open workout-component.html and look for ngModel. Here too, it has only been applied to HTML elements that allow user data input. These include input, textarea, and select. The workout name input setup looks like this:
<input type="text" name="workoutName" class="form-control" id="workout-name" placeholder="Enter workout name. Must be unique." [(ngModel)]="workout.name">
The preceding [(ngModel)] directive sets up a two-way binding between the input control and the workout.name model property. The brackets and parentheses should each look familiar. Previously we used them separately from each other: the [] brackets for property binding and the () parentheses for event binding. In the latter case, we usually bound the event to a call to a method in the component associated with the template. You can see an example of this in the form with the button that a user clicks on to remove an exercise:
<div class="pull-right" (click)="removeExercise(exercisePlan)"><span class="glyphicon glyphicon-trash"></span></div>
Here the click event is explicitly bound to a method called removeExercise in our Workout component class. But for the workout.name input, we do not have an explicit binding to a method on the component. So what's going on here and how does the update happen without us calling a method on the component? The answer to that question is that the combination [( )] is shorthand for both binding a model property to the input element and wiring up an event that updates the model.
Put differently, if we reference a model element in our form, ngModel is smart enough to know that what we want to do is update that element (workout.name here) when a user enters or changes the data in the input field to which it is bound. Under the hood, Angular creates an update method similar to what we would otherwise have to write ourselves. Nice! This approach keeps us from having to write repetitive code to update our model.
Angular supports most of the HTML5 input types, including text, number, select, radio, and checkbox. This means binding between a model and any of these input types just works out of the box.
The textarea element works the same as the input:
<textarea name="description" . . . [(ngModel)]="workout.description"></textarea>
Here we bind textarea to workout.description. Under the hood, ngModel updates the workout description in our model with every change we type into the text area.
To test out how this works, why don't we verify this binding? Add a model interpolation expression against any of the linked inputs such as this one:
<input type="text". . . [(ngModel)]="workout.name">{{workout.name}}
Open the Workout page, type something in the input, and see how the interpolation is updated instantaneously. The magic of two-way binding!
Download
Angular 2 By Example by Chandermani Arora & Kevin Hennessy.mobi
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Hello! Python by Anthony Briggs(9915)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9796)
The Mikado Method by Ola Ellnestam Daniel Brolund(9778)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8297)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7778)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7763)
Grails in Action by Glen Smith Peter Ledbrook(7696)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Windows APT Warfare by Sheng-Hao Ma(6842)
Layered Design for Ruby on Rails Applications by Vladimir Dementyev(6571)
Blueprints Visual Scripting for Unreal Engine 5 - Third Edition by Marcos Romero & Brenden Sewell(6438)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6413)
Kotlin in Action by Dmitry Jemerov(5062)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4317)
Functional Programming in JavaScript by Mantyla Dan(4038)
Solidity Programming Essentials by Ritesh Modi(4003)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3795)
Unity 3D Game Development by Anthony Davis & Travis Baptiste & Russell Craig & Ryan Stunkel(3739)
The Ultimate iOS Interview Playbook by Avi Tsadok(3713)
