如何使用带有http和observables的angular2在json文件中添加/更新/删除产品/项目.以下是我的代码,GET Products工作正常.请告知其他人
产品list.component
export class ProductListComponent implements OnInit {
pageTitle: string = 'Product List';
imageWidth: number = 50;
imageMargin: number = 2;
showImage: boolean = false;
listFilter: string;
errorMessage: string;
products: IProduct[];
constructor(private _productService: ProductService) {
}
toggleImage(): void {
this.showImage = !this.showImage;
}
deleteItem() : void {
this._productService.deleteProduct();
}
ngOnInit(): void {
this._productService.getProducts()
.subscribe(products => this.products = products,
error => this.errorMessage = <any>error);
}
Run Code Online (Sandbox Code Playgroud)
product.service.ts
export class ProductService {
private _productUrl = 'api/products/products.json';
private headers = new Headers({'Content-Type': 'application/json'}); …Run Code Online (Sandbox Code Playgroud)