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

}

相关内容

热门资讯

最新技巧!newpoker怎么... 最新技巧!newpoker怎么安装脚本(辅助挂)好像真的有挂(有挂教学辅助方法)1、玩家可以在线上大...
我来教教你!wepoker辅助... 我来教教你!wepoker辅助器是真的吗(辅助挂)一贯是有挂(真实有挂辅助技巧)我来教教你!wepo...
科技通报!哈糖大菠萝可以开挂吗... 科技通报!哈糖大菠萝可以开挂吗(辅助挂)确实真的是有挂(果真有挂辅助软件)1、模拟器是什么优化,俱乐...
科技新动态!hhpoker破解... 科技新动态!hhpoker破解工具(辅助挂)好像是有挂(有挂秘籍辅助攻略);1、每一步都需要思考,不...
实测揭晓!佛手在线大菠萝智能辅... 实测揭晓!佛手在线大菠萝智能辅助器(辅助挂)切实是有挂(的确有挂辅助工具)1、该软件可以轻松地帮助玩...
玩家必看科普!hhpoker德... 玩家必看科普!hhpoker德州透视(辅助挂)其实存在有挂(有挂秘籍辅助技巧)1、让任何用户在无需安...
我来分享!aa poker辅助... 我来分享!aa poker辅助(辅助挂)都是有挂(有挂讲解辅助方法)1、辅助器安装包、辅助器2、分享...
重大通报!wepoker透视方... 重大通报!wepoker透视方法(辅助挂)本来存在有挂(有挂头条辅助器)1、完成辅助器v3.3的残局...
一分钟了解!hhpoker可以... 一分钟了解!hhpoker可以控制牌吗(辅助挂)本来是有挂(有挂助手辅助方法)1、有没有辅助教程、有...
科普攻略!wepoker如何设... 科普攻略!wepoker如何设置透视(辅助挂)其实有挂(有挂方略辅助攻略)1、超多福利:超高返利,海...