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

}

相关内容

热门资讯

透视玄学!pokemmo手机版... 透视玄学!pokemmo手机版脚本,cloudpoker作弊,扑克教程(有挂插件)1、每一步都需要思...
透视教学!wepoker怎么破... 透视教学!wepoker怎么破解游戏,好像有挂(透视)实用技巧(有挂脚本)1、进入到wepoker怎...
透视安装!约局吧能不能开挂,h... 透视安装!约局吧能不能开挂,hardrock透视工具,解密教程(有挂介绍)1、金币登录送、破产送、升...
透视黑科技!hhpoker真能... 透视黑科技!hhpoker真能买到挂吗,好像存在有挂(透视)微扑克教程(有挂详情)1、hhpoker...
透视计算!pokermaste... 透视计算!pokermaster辅助器,poker world辅助,可靠教程(有挂攻略)运poker...
透视肯定!wepoker透视破... 透视肯定!wepoker透视破解版,都是是真的有挂(透视)wpk教程(有挂辅助)1)wepoker透...
透视中牌率!约局吧透视挂下载,... 透视中牌率!约局吧透视挂下载,竞技联盟透视插件,透明挂教程(有挂介绍)1、每一步都需要思考,不同水平...
透视好友!wpk安卓下载辅助,... 透视好友!wpk安卓下载辅助,原来有挂(透视)2025新版技巧(有挂脚本)1、进入到wpk安卓下载辅...
透视好友!菠萝德州透视脚本,哈... 透视好友!菠萝德州透视脚本,哈糖大菠萝挂法,德州教程(有挂攻略)1)菠萝德州透视脚本辅助挂:进一步探...
透视新版!wepoker有没有... 透视新版!wepoker有没有辅助,本然真的有挂(透视)2025教程(有挂插件)1、透视新版!wep...