Angular中当一个组件中的修改影响到另一个组件时,Angular不会自动更新HTML。
创始人
2024-10-31 01:30:54
0

使用Angular的@ViewChild装饰器和Observables实现组件间的通信。首先,在父组件中定义一个Observable对象,将其传递给子组件,并在子组件中订阅该对象。当子组件中的修改发生时,通过该Observable对象发出通知,将通知发送给父组件,父组件接收到通知后,在调用ViewChild修饰的子组件的方法来更新HTML。

示例代码如下:

// parent.component.ts import { Component, ViewChild } from '@angular/core'; import { Subject } from 'rxjs';

@Component({ selector: 'parent-component', template:

}) export class ParentComponent { @ViewChild('htmlOutput', {static: false}) htmlOutput: ElementRef; subject = new Subject();

onButtonClick() { this.subject.next('Button Clicked'); }

updateHtmlOutput(html: string) { this.htmlOutput.nativeElement.innerHTML = html; }

}

// child.component.ts import { Component, Input } from '@angular/core';

@Component({ selector: 'child-component', template: '' }) export class ChildComponent { @Input() subject: Subject;

ngOnInit() { this.subject.subscribe((msg) => { // handle the message and make updates to the HTML this.updateHtml(msg); }); }

updateHtml(msg: string) { // update the HTML this.htmlOutput.nativeElement.innerHTML = msg; }

}

相关内容

热门资讯

教程攻略!微乐自建房辅助工具,... 教程攻略!微乐自建房辅助工具,微乐自建房脚本免费入口(开挂自建房)app所有人都在同一条线上,像星星...
重大通报!微乐自建房辅助工具免... 重大通报!微乐自建房辅助工具免费app,微乐家乡麻将自建房辅助app(开挂自建房)教程1、重大通报!...
推荐一款!微信微乐自建房辅助工... 推荐一款!微信微乐自建房辅助工具,微乐家乡麻将自建房辅助app(开挂自建房)攻略进入游戏-大厅左侧-...
推荐一款!微乐自建房辅助工具神... 推荐一款!微乐自建房辅助工具神器,微乐小程序免费黑科技(开挂自建房)器1、金币登录送、破产送、升级送...
总算清楚!微信微乐自建房辅助工... 总算清楚!微信微乐自建房辅助工具,微乐家乡麻将自建房辅助app(开挂自建房)app1)微乐家乡麻将自...
重要通知!微乐自建房辅助工具神... 重要通知!微乐自建房辅助工具神器,微乐家乡麻将自建房辅助app(开挂自建房)方法1、在微乐家乡麻将自...
热点推荐!微信微乐自建房辅助工... 热点推荐!微信微乐自建房辅助工具,微乐自建房脚本免费入口(开挂自建房)app微信微乐自建房辅助工具能...
2024教程!微乐自建房辅助工... 2024教程!微乐自建房辅助工具神器,微乐自建房脚本免费入口(开挂自建房)脚本1、微乐自建房辅助工具...
记者揭秘!微乐自建房辅助工具神... 记者揭秘!微乐自建房辅助工具神器,微乐自建房脚本免费入口(开挂自建房)攻略;1、任何微乐自建房辅助工...
玩家实测!微乐自建房辅助工具插... 玩家实测!微乐自建房辅助工具插件,微乐家乡麻将自建房辅助app(开挂自建房)器1、微乐家乡麻将自建房...