Created
November 14, 2016 04:55
-
-
Save jrwebdev/634c95701513dc366cea166f3d72e27d to your computer and use it in GitHub Desktop.
Angular 1 vs Angular 2 Form Validation
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
| // Angular 1 | |
| const module = angular.module('myModule', []); | |
| module.component('myComponent', { | |
| template: ` | |
| <form name="myForm"> | |
| <label for="name">Your Name</label> | |
| <input id="name" name="name" ng-model="$ctrl.name" required /> | |
| <div ng-if="myForm.name.$error">Name is Required</div> | |
| </form> | |
| ` | |
| }); | |
| /***************************************************************/ | |
| // Angular 2 | |
| import {Component} from '@angular/core'; | |
| @Component({ | |
| selector: 'my-component', | |
| template: ` | |
| <form> | |
| <label for="name">Your Name</label> | |
| <input id="name" name="name" [(ngModel)]="name" required #nameField="ngModel" /> | |
| <div *ngIf="!nameField.valid">Name is Required</div> | |
| </form> | |
| ` | |
| }) | |
| class MyComponent { | |
| name: string; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment