当事件监听器更改值时,文本插值不会以角度更新

发布于 2025-01-14 03:38:54 字数 1545 浏览 0 评论 0原文

我有两个角度应用程序,其中一个是父应用程序,另一个是加载子应用程序的 iframe。 HTML 非常基本:

<div class="first">
    <label>{{postedMessage}}</label>
</div>
<div id="second">
    <iframe src="http://localhost:4200/" id="displayFrame" title="Compass" width="70%" 
    float="right" height="750px"></iframe>
</div>

父应用程序正在侦听来自子应用程序的 postMessage() 事件,在本例中为 localhost:4200。我知道 postMessage eventListener 正在工作,因为当父级执行 console.log(JSON.stringify(event)) 时,它会按预期记录。事件监听器也会同时更新postedMessage的值,但它不会在UI上更新。我不明白为什么。我尝试添加一个按钮,在调用 (click) 方法时以相同的方式更新值(除了它不是来自事件),并且其工作原理与预期完全一致。

打字稿如下所示:

import { Component, OnInit } from '@angular/core';

@Component({
  selector: '',
  templateUrl: '',
  styleUrls: ['']
})
export class TestComponent {
  postedMessage: string = 'This is a test string';

    constructor() {
        window.addEventListener("message", this.handleEvent, false);
    }

  handleEvent(event: MessageEvent) {
    console.log(JSON.stringify(event));
    this.postedMessage = (JSON.stringify(event));
  }
}

如果有人知道为什么这不起作用,我们将非常感激!感谢

回答卡尔的回答,我无法提供实际的子应用程序代码。但在最基本的方式中,它看起来像这样:

HTML:

<button (click)="postMessage()">Test</button>

Typescript:

postMessage() {
    // This is where I'd calculate the data being sent to the parent
    data: string; // Some JSON string
    window.parent.postMessage(data, this.parentOrigin);
{

这将是显示正在发生的情况的最简单方法,但如果需要,可以编写更深入的示例。

谢谢!

I have two angular apps one of which is the parent with and iframe that loads the child app. The HTML is extremely basic:

<div class="first">
    <label>{{postedMessage}}</label>
</div>
<div id="second">
    <iframe src="http://localhost:4200/" id="displayFrame" title="Compass" width="70%" 
    float="right" height="750px"></iframe>
</div>

The parent app is listening for a postMessage() event from the child, in this case localhost:4200. I know the postMessage eventListener is working because when the parent does a console.log(JSON.stringify(event)) it logs as expected. The event listener also updates the value of postedMessage at the same time, but it's not updating on the UI. I cannot figure out why. I tried adding a button that updates the value in the same way (except it's not from an event) when the (click) method is called, and that works exactly as expected.

Here is what the typescript looks like:

import { Component, OnInit } from '@angular/core';

@Component({
  selector: '',
  templateUrl: '',
  styleUrls: ['']
})
export class TestComponent {
  postedMessage: string = 'This is a test string';

    constructor() {
        window.addEventListener("message", this.handleEvent, false);
    }

  handleEvent(event: MessageEvent) {
    console.log(JSON.stringify(event));
    this.postedMessage = (JSON.stringify(event));
  }
}

If anyone has any ideas why this wouldn't be working, it would be very much appreciated! Thanks

To answer carl's answer, I cannot provide the actual child app code. In the most basic fashion though, it would look something like this:

HTML:

<button (click)="postMessage()">Test</button>

Typescript:

postMessage() {
    // This is where I'd calculate the data being sent to the parent
    data: string; // Some JSON string
    window.parent.postMessage(data, this.parentOrigin);
{

This would be the simplest way to show what is happening, but can write up a more in depth example if necessary.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽心 2025-01-21 03:38:54

我认为这是因为你需要使你的变量成为可观察流。我认为这将使您的代码正常工作(注意:您需要安装 rxjs 才能使用 BehaviorSubject):

postedMessage: BehaviorSubject<string> = new BehaviorSubject<string>('This is a test string');

为了确保在 handleEvent 中正确引用类组件的范围,请确保绑定 < code>this:

constructor() {
    window.addEventListener("message", this.handleEvent.bind(this), false);
  }

然后在 handleEvent 函数中执行以下操作:

handleEvent(event: MessageEvent) {
    this.postedMessage.next(JSON.stringify(event));
  }

同时使用异步管道进行插值:

<label>{{ postedMessage | async }}</label>

I think it's because you need to make your variable an Observable stream. Here's what I think would make your code work (NOTE: you will need rxjs installed to use BehaviorSubject):

postedMessage: BehaviorSubject<string> = new BehaviorSubject<string>('This is a test string');

To make sure the scope of your class component is referenced correctly in the handleEvent, make sure to bind this:

constructor() {
    window.addEventListener("message", this.handleEvent.bind(this), false);
  }

Then in the handleEvent function do this:

handleEvent(event: MessageEvent) {
    this.postedMessage.next(JSON.stringify(event));
  }

Also use an async pipe for your interpolation:

<label>{{ postedMessage | async }}</label>
高冷爸爸 2025-01-21 03:38:54

在父母和孩子之间建立关系。如何?,
请按照以下步骤操作。

1 - 导入输出
将以下代码放入 child.ts 文件中

import { Component Output, EventEmitter } from '@angular/core';

export class childTestComponent {

@Output() postMessage = new EventEmitter();

}

步骤 2
现在转到 child.html 文件并包含这行代码

<button type="button" (click)="postMessage.emit()">Post Message</button>

第 3 步
现在进入父级的 ts 文件并创建一个发射器,如下所示。发射器只是一个函数,但必须从“on”

onPostMessage() {
    *the rest of click event logic goes here*
     window.alert('button has been clicked');
   }

第 4 步 开始
现在转到parent.html 文件并将发射器绑定到包含子组件的行上的事件,例如,

<childComponent (postMessage)="onPostMessage()"></childComponent>

请告诉我这是否有帮助。

Create a relation between the parent and child. How?,
follow the steps below.

1 - import output
put the the code below in the child.ts file

import { Component Output, EventEmitter } from '@angular/core';

export class childTestComponent {

@Output() postMessage = new EventEmitter();

}

Step 2
Now go to the the child.html file and include this line of code

<button type="button" (click)="postMessage.emit()">Post Message</button>

Step 3
Now got in the parent's ts file and create an emitter like shown below. An Emitter is just a function but must start with "on"

onPostMessage() {
    *the rest of click event logic goes here*
     window.alert('button has been clicked');
   }

Step 4
Now go to the parent.html file and bind the emitter to the event on the line where you include the child component from, eg

<childComponent (postMessage)="onPostMessage()"></childComponent>

Let me know if this helped.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文