At what point does angular2 @output get resolved?

Click on this link

Here is the HTML code snippet:

<chart [options]="options" 
       (load)="saveGraphInstance($event.context)"></chart>
<div (click)="switchGraph()">switch</div>
<div (click)="showLoadingForce()">show loading</div>

This is the Typescript code:

class AppComponent {
    constructor() { 
        this.options = {
            title : { text : 'simple chart' },
            series: [{
            data: [29.9, 71.5, 106.4, 129],
        }],
        lang:{
            loading: 'your text here',
        },
    };
}
options: Object;
graph: any;

switchGraph() {
    this.options = {
        title : { text : 'different chart' },
        series: [{
            data: [129.9, 171.5, 106.4, 129],
        }],
        lang:{
            loading: 'your text here',
        },
    };
    this.graph.showLoading() // This doesn't work
    setTimeout(function() {
        this.graph.showLoading() // This also doesn't work
    }, 4000);
}

showLoadingForce() {
    this.graph.showLoading() // User clicked work
}

saveGraphInstance(graph) {
    this.graph = graph;
    //this.graph.showLoading() // after loading work
}
}

It is observed from the code above that the 'show loading' function does not work if triggered in the same function where the options are changed, even with a timeout of more than 4 seconds.

However, the 'show loading' function works fine if triggered after the 'load' event or initiated by the user.

Here are some questions that arise:

  1. If the 'switch' button is clicked followed by the 'show loading' button immediately, the loading text will appear. Subsequently, the timeout function will run into an error 'ERROR TypeError: Cannot read property 'showLoading' of undefined' after the 4-second delay. How is this possible when 'showLoadingForce' function is successful, implying that 'saveGraphInstance' must have executed?

  2. When does the 'load' event execute and get resolved? The information is not found in the source code on GitHub.

Answer №1

Question 1:

Encountered an ERROR TypeError: Cannot read property 'showLoading' of undefined

This issue arises when trying to access this within a setTimeout() function. The this keyword defaults to the window object within setTimeout. To resolve this, save a reference to this and access it within the setTimeout function.

See more details about the "this" problem.

Regarding Question 2, the 'load' event is defined as an output ChartEvent binding in the ChartComponent, triggering a new instance of EventEmitter.

For hiding the loading image after setTimeout, use the following updated code in the plunker:

 saveGraphInstance(graph) {
      this.graph = graph;
      this.graph.showLoading();
      var that = this;
      setTimeout(function(graph) {
            that.graph.hideLoading();
        }, 4000);
    }

Refer to the JS API Reference

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

Cookie parsing functionality in Node JS malfunctioning

Currently, I am working through a tutorial on cookie management in Express JS found at . The goal is to implement cookies in my web application to authenticate requests to an API that I am constructing with Node JS. To set the cookie upon user login, I emp ...

Learn how to update a form dynamically using the Ajax.BeginForm feature

I am currently working on updating a form within my application using Ajax.BeginForm. The update process is functioning correctly, however, upon executing the function in the controller, it redirects to a view with the controller function name displayed. ...

Angular ng-if directive contains a specific class

I am trying to create a directive that will only be active when an element has a specific class. I have tried the following code: <feedback-analytics ng-if="$('#analyticTab').hasClass('active')"></feedback-analytics> Unfor ...

Obtain the content of a dynamic text input field from a nested directive

I recently developed a custom directive using AngularJS that contains a child directive. The child directive's main task is to create various dynamic input elements like textboxes, radio buttons, and checkboxes, each with default values sourced from a ...

React: Maximum call stack size exceeded error was caught as an uncaught RangeError

I've been experimenting with React and I've managed to get the functionality I want, but it's running very slow due to an infinite loop lurking somewhere. I suspect the issue lies within the component lifecycle methods, but I'm unsure h ...

Finding your way to a particular section within a webpage through an external source

Hey there! I'm currently working on creating a link that will direct users to a specific section within my webpage. For example, redirecting them to https://blabla.github.io/my-website. My code is quite straightforward and it functions properly when ...

Can anyone provide guidance on creating a new type in Ionic Vue?

How can I define the description as a type in my code? When I run it, I get an error that says "Property 'description' does not exist on type '{ takePhoto(): Promise; }'" <script lang="ts"> import { IonPage, ...

Navigating Divs Using jQuery

I am working with a class that has multiple divs, each with a unique id attached to it. I am using jQuery to dynamically cycle through these divs. This is a snippet of my HTML code: <div id ="result">RESULT GOES HERE</div> ...

Getting rid of quotes in a JSON result

My unique code snippet Retrieve data = Array[2] : 0:object id : "1" lat : "76.23" long:"21.92" 1:object id:"2" lat:"10.23" long:"12.92" var newCoords=[]; for(_i = 0; _i < ...

How is the console displaying JavaScript code when I haven't stored it in a separate file?

When trying to trigger a jquery action on an element, an error occurred. Upon checking the console, it displayed an error message: https://i.sstatic.net/5DZSJ.png Interestingly, the code causing the error is not present in my js file at all. Here is a s ...

Ways to prevent recurring variables in Twitter bootstrap dialogues

I need assistance with deleting multiple links using ajax: <a id="id-1">link1</a> <a id="id-2">link2</a> <a id="id-3">link2</a> <a id="id-4">link2</a> ... This is the simplified version of my code: $(docum ...

Is it possible to set up an automatic redirection to the Identity Provider sign-in page when accessing a protected page in Next.js using Auth.js?

Currently in the process of developing a web platform utilizing [email protected] and Auth.js([email protected]). The provider has been configured with the given code, allowing successful signing in using the "Sign in" button. auth.ts import Ne ...

Transmitting OfficeJS 2D Array via an Ajax Call

Struggling with sending a "large" table using OfficeJS: functionfile.html loaded from manifest route <script> (function (){ "use strict"; Office.initialize = function (reason) { $(document).ready(function() { $("#send-data-button").cli ...

What is the best way to modify the colors of two specific elements using a combination of CSS and JavaScript

I am currently developing a browser-based game and have encountered an issue. Here is the relevant line of my HTML/PHP code: echo '<div id="div'.$n.'" class="d'.$rand.'" onclick="swapit(this.id, this.className)"></di ...

Discovering the function invoker when in strict mode

I am facing a challenge in my Angular controller where I have a function called getGames() that can be triggered by both the init() and update() functions. The issue is, I need to handle these two scenarios differently based on which function called getGam ...

Angular: StaticInjectorError(ExplorationEditorPageModule)[Number]

Currently in the process of transitioning oppia's codebase from AngularJS(1.x) to Angular(2+). I recently migrated a service called UtilsService.ts to the following code snippet: import { Injectable } from '@angular/core'; import { downgrad ...

Guide on activating a service in one component to close a pop-up in a separate component [angular]

In order to manage navigation in our app, we have chosen to utilize the Angular router instead of the Ionic navigation controller. One challenge we are facing is handling navigation using the Android back button. When working with the Ionic framework, pre ...

Ways to store Token in Browser Cache?

I am currently developing a login system for an application at my school. I have successfully implemented user registration, which is saved to my Azure DocumentDB. However, when trying to log in with the user, the token does not get saved so that I can acc ...

What could be the reason for the email not being displayed in the form?

I am attempting to automatically populate the user's email in a form when a button is clicked, preferably when the page is loaded. However, I am encountering issues with this process. This project is being developed using Google Apps Script. Code.gs ...

Unusual Observable behavior in Angular/Typescript leaves developers scratching their heads

I have encountered an issue with a single Angular 2 service: validate() { return this.http.get('api/validate', data); } Consuming the API works fine: this.ValidationService.validate().subscribe(result => { console.log(&a ...