Last active
October 29, 2016 10:27
-
-
Save dherges/19a4296b291059b71d055905c8500950 to your computer and use it in GitHub Desktop.
Angular2: reactive v. template-driven form
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component } from '@angular/core' | |
@Component({ | |
selector: 'template-driven-form', | |
template: | |
` | |
<form #myForm="ngForm" (ngSubmit)="submit(myForm.form)"> | |
<div class="form-group"> | |
<label for="td_name" >Name</label> | |
<input type="text" class="form-control" id="td_name" | |
name="name" [ngModel]="''" required> | |
</div> | |
<div class="form-group"> | |
<label for="td_quantity">Quantity</label> | |
<input type="number" class="form-control" id="td_quantity" | |
name="quantity" [ngModel]="123"> | |
</div> | |
<div class="form-group"> | |
<input type="submit" class="form-control" value="Submit" | |
[disabled]="myForm.invalid"> | |
</div> | |
</form> | |
<h6>FormGroup Model (<code>#myForm.form.value</code>)</h6> | |
<div><pre><code>{{ myForm.form.value | json }}</code></pre></div> | |
` | |
}) | |
export class TemplateDrivenFormComponent { | |
submit(form: FormGroup) { | |
console.log("Template-driven form submitted: ", form); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment