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、每一步都需要思考,不...
五分钟了解!好友赣南辅助!果然... 五分钟了解!好友赣南辅助!果然是真的有辅助app(真的有挂)-哔哩哔哩;1、这是跨平台的好友赣南辅助...
第四分钟了解!小程序家乡大贰脚... 第四分钟了解!小程序家乡大贰脚本!其实是真的有辅助攻略(发现有挂)-哔哩哔哩1、下载好小程序家乡大贰...
一分钟了解!钱塘十三水辅助!其... 一分钟了解!钱塘十三水辅助!其实是真的有辅助工具(有人有挂)-哔哩哔哩1、下载好钱塘十三水辅助正确养...
1分钟了解!潮汕掌上娱破解!本... 1分钟了解!潮汕掌上娱破解!本来一直总是有辅助技巧(竟然有挂)-哔哩哔哩1、潮汕掌上娱破解破解器简单...
9分钟了解!财神十三张有挂辅助... 9分钟了解!财神十三张有挂辅助吗!一直真的有辅助app(确实有挂)-哔哩哔哩;1、下载好财神十三张有...
第三分钟了解!丽水都莱有辅助吗... 第三分钟了解!丽水都莱有辅助吗!切实真的有辅助插件(真的有挂)-哔哩哔哩进入游戏-大厅左侧-新手福利...
第7分钟了解!天天贵阳app修... 第7分钟了解!天天贵阳app修改器!本来存在有辅助软件(证实有挂)-哔哩哔哩1、天天贵阳app修改器...
第九分钟了解!微信开发辅助工具... 第九分钟了解!微信开发辅助工具!本来是真的有辅助插件(有挂总结)-哔哩哔哩1、第九分钟了解!微信开发...
5分钟了解!潮汕雀友百度贴吧辅... 5分钟了解!潮汕雀友百度贴吧辅助软件!一贯是有辅助方法(有挂透视)-哔哩哔哩一、潮汕雀友百度贴吧辅助...