ng-bootstrap 모달을 통해 데이터를 추가하고 있지만 추가 버튼을 클릭하면 새로 추가 된 데이터가 표시되기 전에 새로 고침해야하기 때문에 문제가 있습니다. 내가 성공적으로 제품을 추가 할 때 이미)합니다 (this.getMaterials라고하지만 난 새로운 추가 데이터각도를 변경하여 브라우저를 다시로드해야합니다.
export class MaterialsListComponent implements OnInit {
closeResult: string;
materials: any;
subscription: Subscription;
constructor(private modalService: NgbModal, private materialsService: MaterialsService) { }
ngOnInit() {
this.getAllMaterials();
}
getAllMaterials() {
this.subscription = this.materialsService.getAll()
.subscribe(
(data:any) => {
this.materials = data;
console.log(data);
},
error => {
console.log(error);
});
}
onCreateMaterial(form: NgForm){
const name = form.value.name;
const description = form.value.description;
this.materialsService.addMaterial(name, description)
.subscribe(
data => {
this.getAllMaterials();
console.log(data);
},
error => {
console.log(error);
});
}
open(content) {
this.modalService.open(content).result.then((result) => {
this.closeResult = `Closed with: ${result}`;
}, (reason) => {
this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
});
}
private getDismissReason(reason: any): string {
if (reason === ModalDismissReasons.ESC) {
return 'by pressing ESC';
} else if (reason === ModalDismissReasons.BACKDROP_CLICK) {
return 'by clicking on a backdrop';
} else {
return `with: ${reason}`;
}
}
ngOnDestroy() {
this.subscription.unsubscribe();
}
}
서비스
export class MaterialsService {
url = AppSettings;
materials: any;
constructor(private httpClient: HttpClient) {}
getAll() {
if(!this.materials) {
this.materials = this.httpClient.get<any>(this.url)
.map((response => response))
.publishReplay(1)
.refCount();
}
return this.materials;
}
addMaterial(name: string, description: string) {
return this.httpClient
.post(
this.url,
JSON.stringify({ name, description})
)
.map((response: any) => {
return response;
});
}
실시간 데이터를 새로 고치려면 RxJs Subject를 사용해야합니다. http://reactivex.io/documentation/subject.html –
@HaHoang. 내 코드를 도와 주면 좋을 것입니다. 고맙습니다. –