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

}

相关内容

热门资讯

详情透视!aa poker辅助... 详情透视!aa poker辅助,微信小程序微乐陕西挖坑,大纲教程(有挂秘诀)-哔哩哔哩;1、完成aa...
科普透视!wpk私人局有透视吗... 科普透视!wpk私人局有透视吗,微乐江西小程序辅助,资料教程(有挂分享)-哔哩哔哩1)wpk私人局有...
解迷透视!德州圈脚本,微乐四川... 解迷透视!德州圈脚本,微乐四川亲友圈辅助器,技法教程(真的有挂)-哔哩哔哩1、下载好德州圈脚本脚本下...
了解透视!聚星ai辅助工具下载... 了解透视!聚星ai辅助工具下载,微信微乐小程序辅助软件,大纲教程(有挂神器)-哔哩哔哩聚星ai辅助工...
曝光透视!菠萝德州透视脚本,微... 曝光透视!菠萝德州透视脚本,微乐山西脚本插件,手筋教程(真的有挂)-哔哩哔哩1、菠萝德州透视脚本破解...
推荐透视!pokemmo手机辅... 推荐透视!pokemmo手机辅助软件,微乐自建房道具有用吗,窍门教程(有挂头条)-哔哩哔哩pokem...
总结透视!wepoker怎么开... 总结透视!wepoker怎么开辅助,微乐脚本,要领教程(有挂猫腻)-哔哩哔哩1、这是跨平台的wepo...
总结透视!wepoker透视底... 总结透视!wepoker透视底牌,微乐脚本辅助器,教程书教程(有挂存在)-哔哩哔哩1、起透看视 we...
辅助透视!wepoker透视方... 辅助透视!wepoker透视方法,微乐家乡游戏攻略,指南教程(果真有挂)-哔哩哔哩所有人都在同一条线...
了解透视!hhpoker底牌透... 了解透视!hhpoker底牌透视脚本,微乐家乡app下载,要领教程(有挂方法)-哔哩哔哩1、玩家可以...