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

相关内容

热门资讯

透视解谜!德普之星辅助器app... 透视解谜!德普之星辅助器app(透视)拱趴大菠萝开挂方法,教程技法(证实有挂)-哔哩哔哩1)拱趴大菠...
透视解迷!pokerrrr2辅... 透视解迷!pokerrrr2辅助(透视)哈糖大菠萝攻略,教程方案(有挂秘诀)-哔哩哔哩1、哈糖大菠萝...
透视解谜!WePoKer辅助器... 透视解谜!WePoKer辅助器(透视)wepoker私人局俱乐部辅助,教程积累(有挂规律)-哔哩哔哩...
透视开挂!sohoo竞技联盟辅... 透视开挂!sohoo竞技联盟辅助器(透视)德州局透视脚本免费版下载手机版,教程妙计(有挂神器)-哔哩...
透视揭幕!aapoker能控制... 透视揭幕!aapoker能控制牌吗(透视)epoker底牌透视,教程资料(确实有挂)-哔哩哔哩1、玩...
透视分享!wepoker有辅助... 您好,wepoker有辅助器吗这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054...
透视推荐!wepoker软件辅... 透视推荐!wepoker软件辅助程序(透视)wepoker软件辅助程序,教程烘培(有挂解密)-哔哩哔...
透视科普!hhpoker是正规... 透视科普!hhpoker是正规的吗(透视)hhpoker辅助器视频,教程窍要(详细教程)-哔哩哔哩1...
透视解密!xpoker辅助助手... 透视解密!xpoker辅助助手(透视)hhpoker德州挂真的有吗,教程手筋(真的有挂)-哔哩哔哩;...
透视科普!we poker游戏... 透视科普!we poker游戏下(透视)poker world辅助器,教程法子(有挂规律)-哔哩哔哩...