나는 angular2를 사용하는 간단한 CRUD 응용 프로그램을 만들고 있습니다. 제품 목록을 표시하는 ProductListComponent
을 가지고 있으며,이 구성 요소에는 두 개의 하위 구성 요소 ProductDetailComponent
과 ProductUpdateComponent
이 있습니다.보기에서 구성 요소 사이에 업데이트되지 않습니다. 2
ProductUpdateComponent
은 모달을 열어 양식을 제출하고 제출이 성공하면 ProductListComponent
으로 이동합니다. 내가 ProductListComponent
로 이동하고있을 때 그래서 성공적인 업데이트 후, 뷰는
ProductListComponent
@Component({
selector: 'product-list',
moduleId: module.id,
templateUrl: 'product-list.component.html',
directives: [ProductDetailComponent, ProductUpdateComponent],
providers: [ProductService]
})
export class ProductListComponent {
public products: any;
constructor(private productService: ProductService) {
this.productService.getAllProducts()
.subscribe(
response => this.products = response.products
);
}
}
ProductUpdateComponent
(제품 목록이 업데이트되지 않는) 새로운 데이터로 업데이트되지 않는다Component({
selector: 'product-form',
moduleId: module.id,
templateUrl: 'product-form.component.html',
providers: [CheckFormErrors, ProductService]
})
export class ProductFormComponent {
productForm: ControlGroup;
formFields: Array<string>;
view: string;
@Input()
pr: Product;
// form stuff goes here
// after form submitted
saveProduct() {
if (this.productForm.dirty && this.productForm.valid) {
let form = this.productForm.value
let formProduct = new Product({
code: form.code,
name: form.name,
price: form.price,
packing: form.packing,
description: form.description
});
this.productService.updateProduct(this.pr, formProduct)
// navigate to ProductList
this.router.navigate(['/']);
console.log("competed")
}
}
그는 여기에 자세한 전체 코드를 입력하십시오 : https://bitbucket.org/asifpy/inventory-manager/src
ProductService 인스턴스가 여러 공급자 배열에 있으므로 두 인스턴스를 만듭니다. ProductService를 구성 요소 트리에서 상위에있는 하나의'providers' 배열에만 넣으십시오. 그런 다음 ProductListComponent 및 ProductFormComponent는 동일한/단일 ProductService의 인스턴스를 가져옵니다. –
지금 ProductListComponent에서 ProductService를 제거했습니다. ProductListComponent 이제는 제품 서비스가 있지만 stucky는 없습니다. – Asif
productService는 productUpdate 및 productListConponet의 부모에서만 추가해야합니다. –