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

}

相关内容

热门资讯

此事迅速冲上热搜!WePOke... 此事迅速冲上热搜!WePOker有没有透视方法(透视)一贯有辅助平台(有挂透视)-哔哩哔哩1、WeP...
透视了解!wepokerh5破... 透视了解!wepokerh5破解(透视)一贯是有辅助修改器(有挂方针)-哔哩哔哩1、首先打开wepo...
截至目前!wepoker提高好... 截至目前!wepoker提高好牌率(透视)原来是真的辅助软件(今日头条)-哔哩哔哩1、在wepoke...
透视辅助!菠萝辅助器免费版的特... 透视辅助!菠萝辅助器免费版的特点(透视)果然是有辅助app(有挂教学)-哔哩哔哩1、超多福利:超高返...
近期!佛手大菠萝辅助(透视)果... 近期!佛手大菠萝辅助(透视)果然是真的辅助工具(有挂透视)-哔哩哔哩1、每一步都需要思考,不同水平的...
目前!we poker辅助器v... 目前!we poker辅助器v3.3(透视)其实存在有辅助工具(有挂规律)-哔哩哔哩亲,关键说明,w...
近期!佛手在线大菠萝辅助(透视... 近期!佛手在线大菠萝辅助(透视)一贯是有辅助平台(有挂功能)-哔哩哔哩1、全新机制【佛手在线大菠萝辅...
透视挂透视!wepoker透视... 透视挂透视!wepoker透视脚本是什么(透视)切实是有辅助软件(了解有挂)-哔哩哔哩wepoker...
透视app!佛手在线有挂吗(透... 透视app!佛手在线有挂吗(透视)好像真的是有辅助安装(讲解有挂)-哔哩哔哩1、游戏颠覆性的策略玩法...
此事备受玩家关注!来玩app ... 此事备受玩家关注!来玩app 德州 辅助(透视)切实真的有辅助辅助器(有挂实锤)-哔哩哔哩来玩app...