如何将对象从ng bootstrap模式推回到Angular中的主要组件?
正在使用如何将对象从ng bootstrap模式推回到Angular中的主要组件?,angular,ng-bootstrap,Angular,Ng Bootstrap,正在使用@Output装饰器将对象从ng引导模式传输到主(视图)组件。 这是我的HTML(viewProducts.component.HTML),其中显示了一个产品列表,当我单击其中一个时,将调用模式: <tr *ngFor="let product of products> <td class="text-center"> <button class="btn btn-default" name="openPopUp"
@Output
装饰器将对象从ng引导模式传输到主(视图)组件。
这是我的HTML(viewProducts.component.HTML),其中显示了一个产品列表,当我单击其中一个时,将调用模式:
<tr *ngFor="let product of products>
<td class="text-center">
<button class="btn btn-default" name="openPopUp"
(click)="openPopUpEdit(product)"
(callBackPopUp)="callBackPopUp($event)">
<i class="fa fa-edit" style="font-size: 20px"></i>
</button>
</td>
</tr>
viewProducts.component.ts
openPopUpEdit(product) {
this.editProductModalService.open(EditProductComponent as Component,
product);
}
您必须使用服务。以下是一个例子:
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { Observable } from 'rxjs/Observable';
@Injectable()
export class StorageService {
private product: BehaviorSubject<ProductModel> = new BehaviorSubject('');
constructor() { }
getProduct(): Observable<ProductModel> {
return this.product.asObservable();
}
getProductValue(): ProductModel {
return this.product.getValue();
}
setProduct(val: ProductModel) {
this.product.next(val);
}
}
而您的模式必须将该值设置为StorageSevice,而不是发出该值
import {StorageService} from 'storage.service.ts';
constructor(
private storageService: StorageService
){}
save() {
this.service.updateProduct(this.productCopy).subscribe((res) => {
this.storageService.setProduct(res);
}
}
当您从服务中打开模态时,您不能将修改后的对象发射回父组件。要实现这一点,您应该将模态作为父级模板中的子组件。否则,您唯一的机会就是让家长和模型通过另一种服务进行通信。如何让家长和模型通过另一种方式进行通信?如果这个答案有帮助的话,我非常感谢你把这个答案作为解决方案。
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { Observable } from 'rxjs/Observable';
@Injectable()
export class StorageService {
private product: BehaviorSubject<ProductModel> = new BehaviorSubject('');
constructor() { }
getProduct(): Observable<ProductModel> {
return this.product.asObservable();
}
getProductValue(): ProductModel {
return this.product.getValue();
}
setProduct(val: ProductModel) {
this.product.next(val);
}
}
import {StorageService} from 'storage.service.ts';
constructor(
private storageService: StorageService
){}
ngOnInit(): void {
// Update with every change
this.storageService.getProduct().subscribe(product => {
this.modifiedProduct = product;
});
}
import {StorageService} from 'storage.service.ts';
constructor(
private storageService: StorageService
){}
save() {
this.service.updateProduct(this.productCopy).subscribe((res) => {
this.storageService.setProduct(res);
}
}