问题描述: 在Angular模态框中,当数据发生更改后,模态框中的数据未刷新,导致显示的数据和实际数据不一致。
解决方法:
import { Component, Input, OnChanges, SimpleChanges } from '@angular/core';
@Component({
  selector: 'app-modal',
  template: `
    
      
    
  `,
})
export class ModalComponent implements OnChanges {
  @Input() data: string;
  ngOnChanges(changes: SimpleChanges) {
    if (changes.data) {
      this.data = changes.data.currentValue;
    }
  }
}
ngOnInit钩子函数来监听数据的变化并更新模态框的数据。import { Component, OnInit } from '@angular/core';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
@Component({
  selector: 'app-parent-component',
  template: `
    
  `,
})
export class ParentComponent implements OnInit {
  data: string;
  constructor(private modalService: NgbModal) {}
  ngOnInit() {
    // 监听数据变化并更新模态框的数据
    this.data = 'Initial data';
  }
  openModal() {
    const modalRef = this.modalService.open(ModalComponent);
    modalRef.componentInstance.data = this.data;
  }
}
通过以上两个步骤,即可在Angular模态框中实现数据的刷新和更新。