我正在尝试理解一些概念,并想看看是否有人可以提供帮助。我有一个ng-bootstrap模式,它显示一些信息,这些信息是从父组件中进行的API调用输入的。基本上,我将以这种模式显示数据,并且可以点击删除路线以删除一些记录。没关系,但是我的问题是关闭模式时,我希望父组件刷新并且不显示刚刚从模式中删除的记录,因此基本上再次运行请求并更新信息,但是不知道如何做到这一点。我相信通常可以使用事件发射器来完成此操作,但由于我是通过模式并使用ng-bootstrap来执行此操作的,所以我很困惑。任何了解这一点的帮助将不胜感激!
这是我的代码:
app.html:
<div>
<ul>
<li *ngFor="let user of users">{{user.first_name}}</li>
</ul>
<button (click)="openFormModal()" class="btn btn-primary btn-sm">view more</button>
</div>
app.ts:
export class AppComponent {
users: any;
constructor(private api: ApiService,
private modalService: NgbModal) {}
ngOnInit() {
this.getUsers();
}
openFormModal(){
const modalRef = this.modalService.open(DetailsModalComponent);
modalRef.componentInstance.users = this.users;
modalRef.result.then((result)=> {
console.log(result);
}).catch((error) => {
console.log(error);
});
}
getUsers() {
this.api.getUsers().subscribe(data => {
this.users = data;
})
}
}
details-modal.html:
<div class="modal-header">
<h4 class="modal-title">Modal Title</h4>
<button type="button" class="close" aria-label="Close"
(click)="closeModal()">
</button>
</div>
<div class="modal-body">
<p *ngFor="let user of users">{{user.first_name}} <span (click)="deleteUser(this.user.id)" style="cursor: pointer">DELETE</span></p>
</div>
<div class="modal-footer">
<button (click)="closeModal()">
Close Clicked
</button>
</div>
details-modal.ts:
@Component({
selector: 'app-details-modal',
templateUrl: './details-modal.component.html',
styleUrls: ['./details-modal.component.css']
})
export class DetailsModalComponent implements OnInit {
@Input() users;
constructor(
public activeModal: NgbActiveModal,
private api: ApiService
) {}
ngOnInit() {
}
fetch() {
this.api.getUsers().subscribe(data => {
this.users = data;
})
}
closeModal() {
this.activeModal.close()
}
deleteUser(id) {
this.api.deleteUser(id).subscribe(data => {
this.fetch();
})
}
}
答案 0 :(得分:1)
显然,您可以从此GitHub post开始
modalRef.result.then(
() => {
console.log("Close button clicked");
// actions
},
()=> {
console.log("Close icon clicked or backdrop clicked");
// actions
});
Stackblitz:https://stackblitz.com/edit/angular-ey8hkm