Skip to content

Instantly share code, notes, and snippets.

@jhades
Last active December 18, 2020 10:12
Show Gist options
  • Save jhades/939d05ff8e062609a04e7e73bb337e64 to your computer and use it in GitHub Desktop.
Save jhades/939d05ff8e062609a04e7e73bb337e64 to your computer and use it in GitHub Desktop.
Introduction to Angular Forms - Template Driven and Reactive Forms - https://blog.angular-university.io/introduction-to-angular-2-forms-template-driven-vs-model-driven/
<section class="sample-app-content">
<h1>Template-driven Form Example (with bi-directional data binding):</h1>
<form #myForm="ngForm" (ngSubmit)="onSubmitTemplateBased()">
<p>
<label>First Name:</label>
<input type="text"
[(ngModel)]="user.firstName" required>
</p>
<p>
<label>Password:</label>
<input type="password"
[(ngModel)]="user.password" required>
</p>
<p>
<button type="submit" [disabled]="!myForm.valid">Submit</button>
</p>
</form>
</section>
@Component({
selector: "template-driven-form",
templateUrl: 'template-driven-form.html'
})
export class TemplateDrivenForm {
user: Object = {};
onSubmitTemplateBased() {
console.log(this.vm);
}
}
<section class="sample-app-content">
<h1>Reactive Form Example:</h1>
<form [formGroup]="form" (ngSubmit)="onSubmit()">
<p>
<label>First Name:</label>
<input type="text" formControlName="firstName">
</p>
<p>
<label>Password:</label>
<input type="password" formControlName="password">
</p>
<p>
<button type="submit" [disabled]="!form.valid">Submit</button>
</p>
</form>
</section>
import { FormGroup, FormControl, Validators, FormBuilder }
from '@angular/forms';
@Component({
selector: "reactive-form",
templateUrl: 'reactive-form.html'
})
export class ReactiveFormExample {
form = new FormGroup({
"firstName": new FormControl("", Validators.required),
"password": new FormControl("", Validators.required),
});
onSubmitModelBased() {
console.log("reactive form submitted");
console.log(this.form);
}
}
this.form.valueChanges
.pipe(
map((value) => {
value.firstName = value.firstName.toUpperCase();
return value;
}),
filter((value) => this.form.valid)
)
.subscribe((value) => {
console.log("Reactive Form valid value: vm = ",
JSON.stringify(value));
});
<section class="sample-app-content">
<h1>Template-driven Form Example (with one-way data binding):</h1>
<form #myForm="ngForm" (ngSubmit)="onSubmitTemplateBased(myForm.value)">
<p>
<label>First Name:</label>
<input type="text"
[ngModel]="user.firstName" required>
</p>
<p>
<label>Password:</label>
<input type="password"
[ngModel]="user.password" required>
</p>
<p>
<button type="submit" [disabled]="!myForm.valid">Submit</button>
</p>
</form>
</section>
@Component({
selector: "template-driven-form",
templateUrl: 'template-driven-form.html'
})
export class TemplateDrivenForm {
user = {
firstName: 'John',
password: 'test'
};
onSubmitTemplateBased(user) {
console.log(user);
}
}
<section class="sample-app-content">
<h1>Template-driven Form Example (without any type of binding):</h1>
<form #myForm="ngForm" (ngSubmit)="onSubmitTemplateBased(myForm.value)">
<p>
<label>First Name:</label>
<input type="text" ngModel required>
</p>
<p>
<label>Password:</label>
<input type="password" ngModel required>
</p>
<p>
<button type="submit" [disabled]="!myForm.valid">Submit</button>
</p>
</form>
</section>
import {FormsModule} from "@angular/forms";
@Component({...})
export class App { }
@NgModule({
declarations: [App],
imports: [BrowserModule, FormsModule],
bootstrap: [App]
})
export class AppModule {}
import {NgModule} from "@angular/core";
import {ReactiveFormsModule} from "@angular/forms";
@Component({...})
export class App { }
@NgModule({
declarations: [App],
imports: [BrowserModule, ReactiveFormsModule],
bootstrap: [App]
})
export class AppModule {}
<p>
<button type="submit" [disabled]="!form.valid">Submit</button>
<button (click)="partialUpdate()">Partial Update</button>
<button (click)="fullUpdate()">Full Update</button>
<button (click)="reset()">Cancel</button>
</p>
fullUpdate() {
this.form.setValue({firstName: 'Partial', password: 'monkey'});
}
partialUpdate() {
this.form.patchValue({firstName: 'Partial'});
}
reset() {
this.form.reset();
}
import { FormGroup, FormControl, Validators, FormBuilder }
from '@angular/forms';
@Component({
selector: "reactive-form",
templateUrl: 'reactive-form.html'
})
export class ReactiveFormExample {
form = fb.group({
"firstName": ["", Validators.required],
"password":["", Validators.required]
});
constructor(fb: FormBuilder) {
}
onSubmitModelBased() {
console.log("reactive form submitted");
console.log(this.form);
}
}
@alevshunov
Copy link

Probably should be this.user instead of the this.vm.

https://gist.github.com/jhades/939d05ff8e062609a04e7e73bb337e64#file-02-ts-L10

@shifatul-i
Copy link

.filter((value) => this.form.valid)

does not work anymore

@deltaag
Copy link

deltaag commented Dec 2, 2016

Should line 2 of 12.ts be:
this.form.setValue({firstName: 'Partial', password: 'monkey'});
considering the following text of the document http://blog.angular-university.io/introduction-to-angular-2-forms-template-driven-vs-model-driven/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment