Open web-dave opened 6 years ago
ng g c books/book-edit
{
path: ':isbn/edit',
component: BookEditComponent
}
<a [routerLink]="['edit']" class="btn btn-default btn-sm">
edit
</a>
import { FormsModule } from '@angular/forms';
import ...
@NgModule({
imports: [
CommonModule,
BooksRoutingModule,
FormsModule
],
exports: [],
declarations: [...],
providers: [...]
})
export class BooksModule { }
<form *ngIf="book" #form="ngForm" (ngSubmit)="saveBook()">
<div class="form-group">
<label for="title">Title</label>
<input
type="text"
id="title"
name="title"
required minlength="6"
[(ngModel)]="book.title"
#title="ngModel">
<div [hidden]="!title.errors?.required || title.pristine">Enter a Title</div>
</div>
...
<div>
<button type="submit" [disabled]="!form.valid">Save</button>
<a class="btn btn-default btn-sm" [routerLink]="['..']">X</a>
</div>
</form>
saveBook() {
this.booksService.updateBook(this.book)
.subscribe(() => {
this.router.navigate(['..'],{relativeTo:this.route});
});
}
updateBook(book: IBook): Observable<IBook> {
const url = `${this.restRoot}/${book.isbn}`;
return this.http.put<IBook>(url, book);
}
book-edit
componentFormsModule
into BooksModule