Angular's text interpolation fails to update when a value is changed by an eventListener

I am encountering an issue with two angular apps, one acting as the parent and the other as the child within an iframe. The HTML structure is quite simple:

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

The parent app has a postMessage() event listener set up to receive messages from the child app located at localhost:4200. When the event is triggered, it successfully updates the value of postedMessage in the console but does not reflect this change on the UI. Surprisingly, manually updating the value through a button click works perfectly.

Below is a snippet of the TypeScript code:

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 can provide insights into why the UI update is failing, I would greatly appreciate it! Thank you

In response to carl's question, I'm unable to share the exact code of the child app. However, a simplified version of it might include the following:

HTML:

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

TypeScript:

postMessage() {
    // Data calculation for sending to parent
    data: string; // JSON data string
    window.parent.postMessage(data, this.parentOrigin);
{

This simplistic example showcases the communication flow between the parent and child apps. Let me know if more details are needed. Appreciate your help!

Thanks!

Answer №1

One solution could be to transform your variable into an Observable stream. You can achieve this by utilizing the following code snippet (Make sure you have rxjs installed in order to use BehaviorSubject):

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

Ensure the correct referencing of the scope of your class component in the handleEvent function by binding this:

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

Finally, within the handleEvent function, include:

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

Additionally, implement an async pipe for interpolation purposes:

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

Answer №2

Establish a connection between the parent and child elements. Wondering how to do it? Follow these simple steps:

1 - Start by importing the output module and inserting the code below into the child.ts file

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

export class childTestComponent {

@Output() postMessage = new EventEmitter();

}

Step 2 - Next, navigate to the child.html file and add this line of code:

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

Step 3 - Move on to the parent's ts file and define an emitter as shown below. Remember, an Emitter is essentially a function that should begin with "on"

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

Step 4 - Now, head over to the parent.html file and connect the emitter to the event in the line where you're including the child component. For example:

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

If you found this information helpful, feel free to share your feedback!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

In TypeScript, this regular expression dialect does not permit the use of category shorthand

Recently, I attempted to implement a regular expression in TypeScript: I ran the following code: const pass = /^[\pL\pM\pN_-]+$/u.test(control.value) || !control.value; To my surprise, an error occurred: "Category shorthand not allowed in ...

What is the reason behind TypeScript's restriction on referring to const variables in type definitions?

Defining a type that restricts the input string to two possible values can be done like this: type STATE_TYPE = 'DRAFT'|'PUBLISHED' function myFunc(state: STATE_TYPE) { ... } However, when trying to define the two values as const and ...

Are you struggling with perplexing TypeScript error messages caused by a hyphen in the package name?

After creating a JavaScript/TypeScript library, my goal is for it to function as: A global variable when called from either JavaScript or TypeScript Accessible via RequireJS when called from either JavaScript or TypeScript Complete unit test coverage Th ...

Using the hook to implement the useContext function in React

I came across this definition export interface user{ email:string name:string last_name:string } export type UserType= { user: user; setUser:(user:user) => void; } const [user,setUser] = useState <user> ({ email ...

Issue with migrating from Angular version 2.4.10 to 4.0.0

After attempting to update my application from Angular 2.4.10 to 4.0.0, I used the following command: "npm install @angular/common@next @angular/compiler@next @angular/compiler-cli@next @angular/core@next @angular/forms@next @angular/http@next @angular/pl ...

Tips for obtaining the most recent HTML element in Angular

I was able to include HTML content in an Angular (7) UI using the DomSanitizer this.sanitizer.bypassSecurityTrustHtml("htmlstr") Once the content is sanitized and displayed in the HTML view, users have the ability to modify the values as desired ...

Unable to connect with '*ngFor' because it is not a recognized attribute of 'div'

I attempted to bind a value for filter in *ngFor, but it didn't work correctly. How can I successfully bind data to ngFor? Source code is shown below: In my .ts file, menuIcons:any[]; public pageName:any = "projects"; this.menuIcons=[{ ...

You can only use a parameter initializer within the implementation of a function or constructor

I recently started learning TypeScript and am currently using it for React Bricks. I've been working on rendering a 3D object with three.js, but I keep encountering the error mentioned above. I've attempted various solutions such as passing color ...

Issues arising from an aging Angular project

I'm currently facing an issue with installing node and typescript for an older angular project that I have inherited. This project is using angular 2.4.10 After downloading and installing node version 6.9.5 from the executable file, I proceeded to in ...

Tips for selecting the best className type for material-ui components

Currently, I am integrating material-ui into a react app that is built using typescript. Within the material-ui framework, there is a feature called withStyles which allows styles to be injected into a component through its className. However, I am facing ...

Retrieve the instance from the provider using its unique key without needing to inject its dependencies

I created a custom class called PermissionManager, which takes a list of Voter interfaces as an input in its constructor. export interface Voter { vote(): bool; } export class PermissionManager { constructor(private readonly ...

Utilizing import for Ionic3 to export const with logic

While developing an app using ionic3, I encountered an issue with setting up a proxy. When running in a browser, Ionic was able to recognize the path of my proxyUrl as shown below. ionic.config.json { "name": "myApp", "app_id": "", "v2": true, "t ...

What could be the reason behind the error related to react-router-dom?

index.tsx import React from 'react'; import ReactDOM from 'react-dom/client'; import App from './App'; const root = ReactDOM.createRoot( document.getElementById('root') as HTMLElement ); root.render( <React.S ...

Redirecting to child routes based on conditions

I encountered a situation where I need to lazily load child routes and display them conditionally: const routes: Routes = [ { path: '', component: MainComponent, canActivate: [AuthGuard], children: [ { path: &apos ...

The Relationship between Field and Parameter Types in TypeScript

I am currently working on a versatile component that allows for the creation of tables based on column configurations. Each row in the table is represented by a specific data model: export interface Record { attribute1: string, attribute2: { subAt ...

Guide on invoking personalized server-side functions (such as object parsing) utilizing Typescript and Angular tools

I've been grappling for weeks to make custom service calls function with Typescript / Angular / C#. It's been a challenge to find a workable solution online, and the more I search, the more bewildered I become. My current approach has largely be ...

Attempting to retrieve data from cloud Firestore utilizing keyvalue in Angular

My database stores user information under the 'users' collection. I can access this data using the following code: In my service: users$ = this.afs.collection<Users[]>('users').valueChanges(); In my component: public users = t ...

Sharing data between two components in Angular 7

The Address object values are not being retrieved as expected when requesting from the credit card component to a function called getAddress() in a config service that holds the value. Instead of the updated values, I am getting the initial values. Below i ...

Precisely outline the function type that operates on an object, makes changes to its values, and then outputs the object in TypeScript

Can anyone help me create a function that generates a new object with the same keys as the input object, but with all values set to null? Here's the existing code: function nullify(arg) { var returnObj = {} for (var key in arg) { returnObj[ ...

Retrieve the route parameters and exhibit the default option in a dropdown menu using Angular 2/4/5, along with translations implemented through ngx-translate

Is there a way to extract route parameters from a URL and then display them in a drop-down menu? I've attempted some solutions using ActivatedRoute, but they are not returning the first value after the base reference. For instance, If the URL is: l ...