Extending Angular 2 ngModel directive to use observables
Do you really want to create an observable for each input field in your form? The pattern I use is to have one observable for the model of the whole form, clone it for a view variable that you can then bind to and then have the submit handler of the form push the new model back to the service.
user$ = this.userService.user$;
save(user: User) {
this.userService.save(user);
}
and in the view
<form *ngIf="user$ | async | clone as user" #userForm="ngForm" (submit)="userForm.form.valid && save(user)">
<label>
Firstname
<input name="firstname" [(ngModel)]="user.firstname" required>
</label>
<label>
Lastname
<input name="lastname" [(ngModel)]="user.lastname" required>
</label>
<button>Save</button>
</form>
The clone pipe looks like this
export const clone = (obj: any) =>
Array.isArray(obj)
? obj.map(item => clone(item))
: obj instanceof Date
? new Date(obj.getTime())
: obj && typeof obj === 'object'
? Object.getOwnPropertyNames(obj).reduce((o, prop) => {
o[prop] = clone(obj[prop]);
return o;
}, {})
: obj;
import { Pipe, PipeTransform } from '@angular/core';
import { clone } from './clone';
@Pipe({
name: 'clone'
})
export class ClonePipe implements PipeTransform {
transform(value: any): any {
return clone(value);
}
}
I have done a write up on this pattern with my state management library here. https://medium.com/@adrianbrand/angular-state-management-with-rxcache-468a865fc3fb
I came up with a similar approached to @Adbel. Not sure about the inner implications of this, but it will be awesome to have some feedback. Stackbliz code
Your.component.ts
export class AppComponent {
email = new BehaviorSubject("UnwrappedMe ð±");
emailHandler(input) {
this.email.next(input);
}
}
Your.component.html
<form class="mx-3">
<input [ngModel]="email | async"
(ngModelChange)="emailHandler($event)"
name="email" type="email"
id="email" placeholder="Enter email">
</form>
<p class="mx-3"> {{ email | async }} </p>
A little variation in case you need to get a ref to your input value and you do not want to make a second subscription (use template vars).
Your.component.html
<form class="mx-3">
<input [ngModel]="email | async" #emailref
(ngModelChange)="emailHandler($event)"
name="email" type="email"
id="email" placeholder="Enter email">
</form>
<p class="mx-3"> {{ emailref.value }} </p>
I don't know why you wouldn't just use reactive forms, but this was a fun puzzle. I created a directive that will alter the model value to the BehaviorSubject
's value. And any changes will call .next
on the BehaviorSubject
for you.
Usage will look like this
<input type="text" [ngModel]="ngModelValue" appRxModel>
Here is the stackblitz, enjoy