Open web-dave opened 7 years ago
<form [formGroup]="form" (ngSubmit)="saveBook()" >
<div class="form-group">
<label for="title">Title</label>
<input type="text" id="title" formControlName="title" (blur)="checkForm()">
<div [hidden]="!form.get('title').hasError('required')">
Enter a tilte
</div>
</div>
import { FORM_DIRTY, FORM_PRISTINE, IAppState } from '../../redux/store';
import { NgRedux } from '@angular-redux/store';
...
@Component({
selector: 'book-new',
templateUrl: './book-new.component.html',
styleUrls: ['./book-new.component.scss']
})
export class BookNewComponent implements OnInit {
...
constructor(...,
private ngRedux: NgRedux<IAppState>
) { }
}
checkForm() {
if (this.form.dirty) {
this.ngRedux.dispatch(FORM_DIRTY)
} else {
this.ngRedux.dispatch(FORM_PRISTINE)
}
}
saveBook() {
this.booksService.createBook(this.form.value)
.subscribe(() => {
this.ngRedux.dispatch(FORM_PRISTINE)
this.router.navigate(['/books']);
});
}
action
blur
NgRedux
intobook-new
viaDI
dont forget to dispatch on save!!