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)-哔哩哔哩1、实时德州...
透视攻略!德州局透视脚本(透视... 透视攻略!德州局透视脚本(透视)微乐小程序黑科技辅助工具2026最新推荐(一直有辅助app)-哔哩哔...
透视数据!wepoker作必弊... 透视数据!wepoker作必弊视频(透视)贵州微乐自建房辅助软件下载(总是存在有辅助软件)-哔哩哔哩...
2026版方法!wepoker... 2026版方法!wepoker破解版内购(透视)微乐小程序辅助工具2025(其实存在有辅助教程)-哔...
2026版技巧!hhpoker... 2026版技巧!hhpoker真能买到挂吗(透视)微乐手游辅助脚本平台(本来存在有辅助app)-哔哩...
此事迅速冲上热搜!hhpoke... 此事迅速冲上热搜!hhpoker外开挂靠谱吗(透视)微乐广西小程序(一贯真的有辅助工具)-哔哩哔哩1...
透视策略!wpk透视辅助方法(... 透视策略!wpk透视辅助方法(德普之星破解版)果然真的有辅助app(哔哩哔哩)1、用户打开应用后不用...
透视辅助!pokerworld... 您好,pokerworld破解版下载这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275...
透视办法!wpk辅助(WePo... 透视办法!wpk辅助(WePoKer功能)一直有辅助软件(哔哩哔哩)wpk辅助破解侠是真的助透视。每...
据监测!wepoker私人局透... 据监测!wepoker私人局透视方法(透视)微乐小程序游戏破解器下载(好像是真的辅助app)-哔哩哔...