Skip to content

Instantly share code, notes, and snippets.

@jrwebdev
Created November 14, 2016 04:55
Show Gist options
  • Select an option

  • Save jrwebdev/634c95701513dc366cea166f3d72e27d to your computer and use it in GitHub Desktop.

Select an option

Save jrwebdev/634c95701513dc366cea166f3d72e27d to your computer and use it in GitHub Desktop.
Angular 1 vs Angular 2 Form Validation
// 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