在Angular中,如果需要在模态框中显示确认框,可以使用ng-bootstrap库中提供的ngbModal方法。具体实现步骤如下:
npm install @ng-bootstrap/ng-bootstrap
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
@NgModule({
imports: [ NgbModule ]
})
export class AppModule {}
openConfirmationModal() {
const modalRef = this.modalService.open(ConfirmationModalComponent);
modalRef.result.then((result) => {
//确认按钮回调函数
}).catch((error) => {
//取消按钮回调函数
});
}
例如,在模态框组件html文件中添加以下代码:
Confirmation
Are you sure you want to do this?
例如,在模态框组件ts文件中添加以下代码:
yes() {
this.modalService.dismissAll('yes');
}
no() {
this.modalService.dismissAll('no');
}
close() {
this.modalService.dismissAll('cancel');
}
上一篇:Angular确定的日期格式错误
下一篇:Angular缺少依赖