[FIXED] Angular Primeng Menubar Access To Method Problem

Issue

I’m trying to call a method on the menu onclick event as follows, but it gives an error.

component.ts

    loadWorkSpace(WorkSpaceName: string, QNameSpaceId: number, QEnvironemntId: number, QWorkSpaceId: number) {
this._qWorkSpaceFilterServiceProxy
                    .getAll(QWorkSpaceId, null, 0, 1000)
                    .subscribe((result) => {
                        filterItemList = _map(result.items, function (qWSFilter) {
                            return {
                                label: qWSFilter.qWorkSpaceFilter.workspaceFilterName, command: (event: Event) => this.consoleMsg()
                            };
                        });

                        this.items = [
                            {
                                label: 'Applications',
                                items: itemList
                            },
                            {
                                label: 'Filters',
                                items: filterItemList
                            }
                        ];
                    });}

    consoleMsg() {
        console.log("test");
    }

Error:
Console error message

I’m sorry for my bad english.

Solution

I was having trouble accessing the ‘this’ token because of the ‘_map’ method I used.

Doing it this way fixed it:

filterItemList = result.items.map((qWSFilter) => {
                            return {
                                label: qWSFilter.qWorkSpaceFilter.workspaceFilterName, command: (event) => { this.consoleMsg() }
                            };
                        });

Answered By – Türk Yazılımcı

Answer Checked By – Timothy Miller (Easybugfix Admin)

Leave a Reply

(*) Required, Your email will not be published