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模态框中实现数据的刷新和更新。

相关内容

热门资讯

透视窍门!wepoker透视版... 透视窍门!wepoker透视版下载(透视)其实真的是有辅助神器(哔哩哔哩)1、进入到wepoker透...
透视手筋!aapoker透视怎... 透视手筋!aapoker透视怎么用(透视)一直是真的辅助插件(哔哩哔哩)1、游戏颠覆性的策略玩法,独...
透视课程!wepoker透视方... 透视课程!wepoker透视方法(透视)确实有辅助下载(哔哩哔哩)1、wepoker透视方法辅助软件...
透视演示!哈糖大菠萝有没有挂(... 透视演示!哈糖大菠萝有没有挂(透视)其实是有辅助安装(哔哩哔哩)哈糖大菠萝有没有挂脚本下载中分为三种...
透视方针!hhpoker有没有... 透视方针!hhpoker有没有外开挂(透视)原来是真的辅助工具(哔哩哔哩)一、hhpoker有没有外...
透视步骤!约局吧app有挂吗(... 透视步骤!约局吧app有挂吗(透视)其实是有辅助app(哔哩哔哩)1、约局吧app有挂吗免费脚本咨询...
透视窍门!wepoker怎么发... 透视窍门!wepoker怎么发冤家牌(透视)好像是有辅助神器(哔哩哔哩);1、透视窍门!wepoke...
透视机巧!werplan脚本(... 透视机巧!werplan脚本(透视)真是是真的辅助安装(哔哩哔哩)1、不需要AI权限,帮助你快速的进...
透视学习!大菠萝手游辅助(透视... 透视学习!大菠萝手游辅助(透视)真是是有辅助修改器(哔哩哔哩)1、进入游戏-大厅左侧-新手福利-激活...
透视策略!购买的wpk辅助在哪... 透视策略!购买的wpk辅助在哪里下载(透视)切实有辅助工具(哔哩哔哩)1、金币登录送、破产送、升级送...