-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from actionanand/features/1-template-driven
Features/1 template driven
- Loading branch information
Showing
4 changed files
with
120 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,38 @@ | ||
<form> | ||
<form #formEl="ngForm" (ngSubmit)="onSubmit(formEl)"> | ||
<h2>Login</h2> | ||
|
||
<div class="control-row"> | ||
<div class="control no-margin"> | ||
<label for="email">Email</label> | ||
<input id="email" type="email" /> | ||
<input id="email" type="email" name="emailField" ngModel required email #emailCtrl="ngModel" /> | ||
</div> | ||
|
||
<div class="control no-margin"> | ||
<label for="password">Password</label> | ||
<input id="password" type="password" /> | ||
<input | ||
id="password" | ||
type="password" | ||
name="passField" | ||
ngModel | ||
required | ||
[minlength]="minPassLength" | ||
#passwordCtrl="ngModel" /> | ||
</div> | ||
|
||
<button class="button">Login</button> | ||
</div> | ||
|
||
<!-- @if(formEl.form.invalid && formEl.form.controls['emailField'].touched) { | ||
<p class="control-error"> | ||
Invalid email entered | ||
</p> | ||
} --> | ||
|
||
@if (emailCtrl.touched && emailCtrl.dirty && emailCtrl.invalid) { | ||
<p class="control-error">Invalid email address entered!</p> | ||
} | ||
|
||
@if (passwordCtrl.touched && passwordCtrl.dirty && passwordCtrl.invalid) { | ||
<p class="control-error">Password must be atleast {{ minPassLength }} characters!</p> | ||
} | ||
</form> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,63 @@ | ||
import { Component } from '@angular/core'; | ||
import { afterNextRender, Component, DestroyRef, inject, viewChild } from '@angular/core'; | ||
import { FormsModule, NgForm } from '@angular/forms'; | ||
|
||
import { debounceTime } from 'rxjs/operators'; | ||
|
||
@Component({ | ||
selector: 'app-login', | ||
standalone: true, | ||
imports: [FormsModule], | ||
templateUrl: './login.component.html', | ||
styleUrl: './login.component.css', | ||
}) | ||
export class LoginComponent {} | ||
export class LoginComponent { | ||
minPassLength = 5; | ||
private formObj = viewChild.required<NgForm>('formEl'); | ||
private destroyRef = inject(DestroyRef); | ||
|
||
constructor() { | ||
afterNextRender(() => { | ||
const localFormVal = localStorage.getItem('saved-login-form'); | ||
|
||
if (localFormVal) { | ||
const savedFormEmail = JSON.parse(localFormVal).email; | ||
|
||
/* // to set value for whole form | ||
setTimeout(() => { | ||
this.formObj().setValue({ | ||
emailField: savedFormEmail, | ||
passField: '', | ||
}); | ||
}, 1); */ | ||
|
||
setTimeout(() => { | ||
this.formObj().controls['emailField'].setValue(savedFormEmail); | ||
}, 1); | ||
} | ||
|
||
const formSub = this.formObj() | ||
.valueChanges?.pipe(debounceTime(1000)) | ||
.subscribe({ | ||
next: form => { | ||
localStorage.setItem('saved-login-form', JSON.stringify({ email: form.emailField })); | ||
}, | ||
}); | ||
|
||
this.destroyRef.onDestroy(() => formSub?.unsubscribe()); | ||
}); | ||
} | ||
|
||
onSubmit(formEl: NgForm) { | ||
if (formEl.form.invalid) { | ||
return; | ||
} | ||
|
||
console.log(formEl); | ||
|
||
console.log(formEl.form.controls['emailField']); | ||
console.log(formEl.form.value['emailField']); | ||
|
||
formEl.form.reset(); | ||
} | ||
} |
This file contains 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