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网页版(wep... 记者爆料!wpk网页版(wepOkE)外挂透明挂辅助工具(透视)详细教程(确实有挂)-哔哩哔哩这是由...
一分钟了解(wepoke好友)... 一分钟了解(wepoke好友)外挂透明挂辅助器(辅助挂)透视辅助(有挂技术)-哔哩哔哩关于wepok...
透视辅助(德州nzt软件)外挂... 透视辅助(德州nzt软件)外挂透明挂辅助软件(辅助挂)发牌机制(有挂头条)-哔哩哔哩;人气非常高,a...
技术分享(扑克世界牌局设置)外... 技术分享(扑克世界牌局设置)外挂透明挂辅助APP(透视)新2024版(2020已更新)(哔哩哔哩);...
盘点十款!德州之星app(we... 1、盘点十款!德州之星app(wePoKe)外挂透明挂辅助挂(辅助挂)黑科技教程(有挂功能)-哔哩哔...
必看攻略(gg扑克软件)外挂透... 必看攻略(gg扑克软件)外挂透明挂辅助软件(透视)软件透明挂(有挂分析)-哔哩哔哩是一款可以让一直输...
研究成果(wEpoKe)外挂透... 研究成果(wEpoKe)外挂透明挂辅助APP(透视)透牌教程(2022已更新)(哔哩哔哩)1、点击下...
分享实测(扑克时间软件)外挂透... 分享实测(扑克时间软件)外挂透明挂辅助脚本(透视)透视辅助(有挂教学)-哔哩哔哩;原来确实真的有挂(...
最新技巧!德扑查数据(wEpo... 最新技巧!德扑查数据(wEpoKe)外挂透明挂辅助插件(透视)2024新版教程(果真有挂)-哔哩哔哩...
大家学习交流(德扑之星开桌)外... 大家学习交流(德扑之星开桌)外挂透明挂辅助器(透视)软件透明挂(有挂规律)-哔哩哔哩是一款可以让一直...