从 ng-bootstrap Modal 中删除后用新数据重新加载组件
Reload component with new data after deleting from ng-bootstrap Modal
我正在尝试理解一些概念,想看看是否有人可以提供帮助。我有一个 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;
})
}
}
详情-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>
详情-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();
})
}
}
显然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
我正在尝试理解一些概念,想看看是否有人可以提供帮助。我有一个 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;
})
}
}
详情-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>
详情-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();
})
}
}
显然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