【问题标题】:Cannot close ng-bootstrap Modal无法关闭 ng-bootstrap 模态
【发布时间】:2017-03-14 02:07:59
【问题描述】:

所以我有一个NgbModal,里面有一个表单,我想要实现的是在成功提交时关闭它。

这是我的模态组件:

@Component({
    selector: 'create-update-transaction',
    templateUrl: './CreateOrUpdateTransaction.html',
    providers: [AccountTransactionsService]
})
export class CreateOrUpdateTransactionComponent {
    closeResult: string;
    modalRef: NgbModalRef;

    @Input() transaction: Transaction = new Transaction();
    @Output() onSubmit: EventEmitter<void> = new EventEmitter<void>();

    constructor(private modalService: NgbModal,
                private transactionsService: AccountTransactionsService) {}

    sendTransaction(): void{
        let localModalRef = this.modalRef;
        this.transactionsService.createOrUpdateTransaction(this.transaction, (isSuccessful)=>{
            if (isSuccessful) {
                this.onSubmit.emit();
                localModalRef.close(); //<--- The problem is here
            }
        });
    }

    open(content) {
        this.modalRef = this.modalService.open(content).result.then((result) => {
            this.closeResult = `Closed with: ${result}`;
        }, (reason) => {
            this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
        });
    }
}

我在尝试调用已保存的NgbModalRef 的关闭方法时收到localModalRef.close is not a function 错误

【问题讨论】:

    标签: javascript angular modal-dialog ng-bootstrap


    【解决方案1】:

    这应该适合你:

    open(content) {
      this.modalRef = this.modalService.open(content);
      this.modalRef.result.then((result) => {
        this.closeResult = `Closed with: ${result}`;
      }, (reason) => {
          this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
      });
    }
    

    否则您的modalRef 变量将引用ZoneAwarePromise 对象

    【讨论】:

    • 您会认为 ng-bootstrap 网站向您展示了如何执行此操作。干杯
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多