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

}

相关内容

热门资讯

科技介绍!wepoker有没有... 科技介绍!wepoker有没有插件(辅助挂)确实真的是有挂(有挂总结)1、进入到wepoker有没有...
一起来讨论!哈糖大菠萝有没有挂... 一起来讨论!哈糖大菠萝有没有挂(辅助挂)切实存在有挂(揭秘有挂)1、超多福利:超高返利,海量正版游戏...
最新通报!德普之星透视辅助软件... 最新通报!德普之星透视辅助软件(辅助挂)确实真的有挂(真的有挂)1、任何德普之星透视辅助软件透视是真...
终于知道!epoker免费透视... 终于知道!epoker免费透视脚本(辅助挂)都是有挂(确实有挂)所有人都在同一条线上,像星星一样排成...
盘点一款!wepoker底牌透... 盘点一款!wepoker底牌透视(辅助挂)确实存在有挂(有挂教程)1、wepoker底牌透视免费辅助...
交流学习经验!steampok... 交流学习经验!steampokermaster辅助(辅助挂)原来有挂(有挂详细)一、steampok...
解密关于!wepoker破解游... 解密关于!wepoker破解游戏盒子(辅助挂)竟然真的是有挂(有挂详情)1、进入游戏-大厅左侧-新手...
记者揭秘!aapoker怎么提... 记者揭秘!aapoker怎么提高中牌率(辅助挂)确实真的有挂(讲解有挂)1、每一步都需要思考,不同水...
分享实测!wepoker辅助插... 分享实测!wepoker辅助插件功能(辅助挂)真是有挂(有挂方针)亲,关键说明,wepoker辅助插...
总算了解!聚星ai辅助工具收费... 总算了解!聚星ai辅助工具收费多少(辅助挂)一贯存在有挂(讲解有挂)总算了解!聚星ai辅助工具收费多...