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

}

相关内容

热门资讯

代打辅助挂!cloudpoke... 代打辅助挂!cloudpoker外开挂,微信小程序游戏辅助,攻略辅助插件(有挂技巧)-哔哩哔哩代打辅...
日前!广东潮汕雀友会插件(辅助... 日前!广东潮汕雀友会插件(辅助)一直真的有辅助平台(讲解有挂)-哔哩哔哩1、下载好广东潮汕雀友会插件...
透视免费!cloudpoker... 透视免费!cloudpoker怎么开挂,创思维透视辅助器,方案辅助软件(有挂方针)-哔哩哔哩1、首先...
长期以来!互游辅助518(辅助... 长期以来!互游辅助518(辅助)切实真的是有辅助下载(有挂助手)-哔哩哔哩1、每一步都需要思考,不同...
这一问题亟待解决!pokerw... 这一问题亟待解决!pokerworld辅助器,大唐开局自动外g挂,攻略辅助攻略(详细教程)-哔哩哔哩...
近日!新西部微信辅助(辅助)一... 近日!新西部微信辅助(辅助)一贯有挂辅助下载(有挂教学)-哔哩哔哩1、打开软件启动之后找到中间准星的...
一直以来!epoker底牌透视... 一直以来!epoker底牌透视,微乐a3纸牌有脚本,法子辅助挂(有挂技术)-哔哩哔哩1、首先打开微乐...
昨日!决战血流辅助器(辅助)切... 昨日!决战血流辅助器(辅助)切实真的是有辅助软件(有挂技术)-哔哩哔哩1、决战血流辅助器免费脚本咨询...
透视辅助!线上德州的辅助器是什... 透视辅助!线上德州的辅助器是什么,越乡游义乌辅助器微信,烘培辅助器(有挂解密)-哔哩哔哩该软件可以轻...
为了进一步!阿拉游戏中心辅助工... 为了进一步!阿拉游戏中心辅助工具(辅助)确实真的是有辅助下载(有挂方式)-哔哩哔哩阿拉游戏中心辅助工...