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; }

}

相关内容

热门资讯

透视科普!wpk模拟器,wpk... 透视科普!wpk模拟器,wpk系统是否存在作弊行为-本来存在有辅助插件(哔哩哔哩)1、任何透视是真的...
透视详情!wepoker看底牌... 透视详情!wepoker看底牌,wejoker辅助器怎么卖-真是有辅助软件(哔哩哔哩)1、玩家可以在...
透视揭露!佛手在线是不是有挂,... 透视揭露!佛手在线是不是有挂,拱趴大菠萝有什么挂-真是一直都是有辅助技巧(哔哩哔哩)1、完成佛手在线...
透视推荐!hhpoker免费透... 透视推荐!hhpoker免费透视脚本,hhpoker作弊实战视频-原来是真的有辅助软件(哔哩哔哩)1...
透视分享!hhpoker德州透... 透视分享!hhpoker德州透视,hhpoker视频巡查真的假的-一贯一直都是有辅助app(哔哩哔哩...
透视解谜!wpk辅助软件,wp... 透视解谜!wpk辅助软件,wpk辅助购买-都是是有辅助插件(哔哩哔哩)1、用户打开应用后不用登录就可...
透视详情!德扑圈有透视吗,如何... 透视详情!德扑圈有透视吗,如何下载德普之星辅助软件-切实一直都是有辅助教程(哔哩哔哩)1、有没有辅助...
透视详情!德普辅助器怎么用,德... 透视详情!德普辅助器怎么用,德普之星怎么开辅助-都是是真的有辅助插件(哔哩哔哩)1、透视详情!德普辅...
透视揭幕!德普之星有辅助软件吗... 透视揭幕!德普之星有辅助软件吗,德普之星透视辅助软件-其实是有辅助工具(哔哩哔哩)暗藏猫腻,小编详细...
透视解密!wepoker轻量版... 透视解密!wepoker轻量版透视,wepoker透视脚本下载-本来是有辅助神器(哔哩哔哩)1、完成...