angular - How to apply form validation without <form> tag in Angular2 -
i wondering if there way validate form in angular 2 without using form
tag? example below want make required field
<div class="form-group"> <label for="name">name</label> <input type="text" class="form-control" id="name"> </div> <button type="button" class="btn btn-default" (click)="save()">save</button>
form controls can standalone (without parent form), whether using declarative form controls or reactive form controls.
for declarative (using ngmodel
) can like
<input #model="ngmodel" [(ngmodel)]="value" type="text" required/> <div *ngif="model.invalid" style="color: red">required</div>
for reactive forms can like
<input [formcontrol]="control" [(ngmodel)]="value" type="text"/> <div *ngif="control.invalid" style="color: red">required</div> // in component this.control = new formcontrol('', [validators.required]);
see plunker
for more information on using angular forms in general, see the docs/tutorial
Comments
Post a Comment