Angular模态框中的数据在数据更改后未刷新。
创始人
2024-10-28 15:31:43
0

问题描述: 在Angular模态框中,当数据发生更改后,模态框中的数据未刷新,导致显示的数据和实际数据不一致。

解决方法:

  1. 在模态框组件中,通过订阅数据变化事件来刷新数据。
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;
    }
  }
}
  1. 在调用模态框的组件中,使用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模态框中实现数据的刷新和更新。

相关内容

热门资讯

必看透视"wepok... 必看透视"wepoker透视辅助教程"开挂(透视)辅助神器(记者揭秘软件教程)必看透视"wepoke...
分享透视"wepok... 分享透视"wepoker真的能透视吗"开挂(透视)辅助下载(玩家必备教程2026新版教程)分享透视"...
必看透视"德州wep... 必看透视"德州wepoker透视挂"开挂(透视)辅助下载(程序员教你解密教程)必看透视"德州wepo...
正版透视"微扑克的辅... 正版透视"微扑克的辅助工具有哪些"开挂(透视)辅助插件(记者发布解说技巧)正版透视"微扑克的辅助工具...
实测透视"微扑克辅助... 实测透视"微扑克辅助器破解器"开挂(透视)辅助脚本(六分钟了解教你攻略)实测透视"微扑克辅助器破解器...
分享透视"wepok... 分享透视"wepoker透视挂的玩法"开挂(透视)辅助器(科普分享黑科技教程)分享透视"wepoke...
细节透视"有wepo... 细节透视"有wepoker透视吗"开挂(透视)辅助下载(记者揭秘透明挂教程)细节透视"有wepoke...
教会透视"wepok... 教会透视"wepokerplus透视挂真的假的"开挂(透视)辅助软件(发现玩家辅助教程)教会透视"w...
必看透视"WePoK... 必看透视"WePoKer辅助透视软件挂"开挂(透视)辅助挂(玩家亲测德州论坛)必看透视"WePoKe...
传授透视"wpk到底... 传授透视"wpk到底有没有辅助挂"开挂(透视)辅助下载(技术分享靠谱教程)传授透视"wpk到底有没有...