1

Tried to assign a event in a variable but not working.I do not know how to assign.I am getting in console.log(this.myEvents) like null and not able to access closest tr. If anyone know please help to find the solution.

app.component.ts:

public myEvents:any; 
logEvent(e)
{ 
    this.myEvents=e; // Store event info
}

// Set a timer for looping through the events
gameTimer = setInterval(function() {
    // Loop through events
    if(this.myEvents)
    {
        console.log(this.myEvents);// getting null

    }
}, 100)​

app.component.html:

<button (click)="logEvent(e)">Set event</button>

Demo: https://stackblitz.com/edit/angular-yqelod?file=src/app/app.component.ts

Nila Vaani
  • 203
  • 1
  • 8
  • 27
  • 1
    What's the problem? – VLAZ Feb 07 '20 at 12:32
  • @VLAZ: How to save the event in a variable – Nila Vaani Feb 07 '20 at 12:36
  • But what's the problem with the code you have here? What doesn't work? Do you get any errors? Do you get unexpected behaviour? What do you expect the behaviour to be? Saying "doesn't work" is not helpful in trying to determine what is wrong and how to fix it. – VLAZ Feb 07 '20 at 12:36
  • @VLAZ: Edited my question..I am getting this.myEvents is null – Nila Vaani Feb 07 '20 at 12:41

2 Answers2

0

You have to use arrow function in the setInterval () => or the this pointer is not showing on your component and you can't assign to this.myEvents.

I fixed your component without testing:

class Game implements OnInit, OnDestroy {
    public myEvents: any; 

    private gameTimer: any = null;

    constructor() {

    }

     // Set a timer for looping through the events
    ngOnInit() {
        this.gameTimer = setInterval(() => {
            // Loop through events
            if (this.myEvents) {
                console.log(this.myEvents);// getting null

            }
        }, 100);​
    }

    ngOnDestroy() {
        if (this.gameTimer !== null) {
            clearInterval(this.gameTimer);
        }
    }

    logEvent(e: any)
    { 
        this.myEvents = e; // Store event info
    }

}

important is the part:

           this.gameTimer = setInterval(() => {
                // Loop through events
                if (this.myEvents) {
                    console.log(this.myEvents);// getting null

                }
            }, 100);​

Update: Working stackblitz example: https://stackblitz.com/edit/angular-xcjuqn

Seryoga
  • 793
  • 6
  • 15
  • How to access closest tr of button?? – Nila Vaani Feb 07 '20 at 12:56
  • Not working : https://stackblitz.com/edit/angular-yqelod?file=src/app/app.component.ts – Nila Vaani Feb 07 '20 at 13:08
  • You have any solution? – Nila Vaani Feb 07 '20 at 13:18
  • The event object has a value `path` its an array of elements between your `button` and `html` but its not supported in all browser maybe you could find polyfill: https://stackblitz.com/edit/angular-mpk69o ... I created a function in the stackblitz to find your desired element from the event – Seryoga Feb 07 '20 at 13:25
0

In your template file,

Use

<button (click)="logEvent($event)">Set event</button>

Instead of

<button (click)="logEvent(e)">Set event</button>

It might help you.