What could be the reason for the file element being undefined in the context menu?

I am currently working on rebuilding my context menu for the second time today. I am encountering an issue with an undefined value of my file element, which is preventing me from deleting, renaming, or performing any other actions.

HTML

<mat-list-item *ngFor="let element of fileElements" (click)="navigate(element)" (contextmenu)="onContextMenu($event, element)" >
        [...]
    </mat-list-item>
[...]
<div style="visibility: hidden; position: fixed"
    [style.left]="contextMenuPosition.x"
    [style.top]="contextMenuPosition.y"
    [matMenuTriggerFor]="contextMenuTag">
</div>
<mat-menu #contextMenuTag="matMenu">
    <ng-template matMenuContent let-item>
    <div *ngFor="let element of contextMenu">
      <button *ngIf="!element.seperator" mat-menu-item (click)="callFunction(element, item)">
        {{element.name}}
      </button>
      <mat-divider *ngIf="element.seperator"></mat-divider>
    </div>
    </ng-template>
</mat-menu>

Typescript

public callFunction(menu: MenuElement, file?: FileElement): void {
    console.log(file);  // <-- UNDEFINED THERE
    switch(menu.action) {
      case 'delete': {
        this.deleteElement(file);
        break;
      }
      [...]
    }
}

private deleteElement(element: FileElement): void {
    this.elementRemoved.emit(element);
}

Prior to making changes to my previously implemented context menu, the delete action was functional. I suspect there may be a problem with passing `(contextmenu)="onContextMenu($event, element)"` to the mat-menu at the bottom. Perhaps someone has encountered this issue before?

Answer №1

Initially, there was a situation where the variable was not named item. By making some adjustments in your code, you will discover that using matMenuTriggerData will allow you to bind the data to your ng-template.

<div style="visibility: hidden; position: fixed"
    [style.left]="contextMenuPosition.x"
    [style.top]="contextMenuPosition.y"
    [matMenuTriggerFor]="contextMenuTag"
    [matMenuTriggerData]="{item:theitemfromyourcode}" >
</div>

Furthermore, remember to assign the value of let-item to your variable, which should be defined in your TypeScript file. Failure to do so may result in the item being bound to $implict.

<ng-template matMenuContent let-item="item">
[...]
</ng-template>

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

The BullMQ library is optimizing performance by efficiently managing Redis connections

Currently, I'm in the process of implementing logic to efficiently reuse redis connections with bullMQ by referring to this specific section in the bullMQ documentation. My setup involves utilizing the latest BullMQ npm version (1.80.6). As per the ...

Display a loading indicator in Angular during app initialization with APP_INITIALIZER

Is there a way to display a waiting indicator while the Angular app is running the app_initializer code? Currently, I can display a waiting indicator until the app is fully loaded. However, once the page loads, it remains empty until the app_initializer c ...

Error: Typings: Invalid syntax - Unexpected symbol =>

Every time I run a typings command, I encounter the following error: AppData\Roaming\npm\node_modules\typings\node_modules\strip-bom\index.js:2 module.exports = x => { ^^ SyntaxError: Unexpected tok ...

Image paths becoming unresponsive following package upgrades

My Angular2 application was originally built using angular-cli (v 1.0.0) and webpack2. Within a component, I had the ability to reference an image like so: <div class="country-flag"> <img [src]="src/assets/flags/32/jp.png" [width]="flagIconSiz ...

The arrow function in Jest is missing a name property

Currently, my setup includes: node.js: 9.8.0 Jest: 23.4.2 ts-jest: 23.1.3 typescript: 2.9.2 While attempting the following in my *.test.ts files: const foo = () => 'bar'; console.log(foo.name); // '' foo contains the name pro ...

Error encountered while upgrading to Angular 5: splitHash issue

Currently in the process of transitioning from Angular 4.x to 5.x, I have encountered the following error: main.81bcdf404dc22078865d.bundle.js:1 Uncaught TypeError: i.splitHash is not a function at Object.t.parseUrl (main.81bcdf404dc22078865d.bundle.js:1) ...

Tips for passing the actions payload within an ngrx effect to the WiithLatestFrom() function in order to retrieve a specific value from the state

Hey there, struggling to pass my actions payload to getMainCareuserMessagesConversationAppointmentsShift(?) with the following effect. caregiverAcceptFetch = createEffect(() => { return this.actions.pipe( ofType(rdxMainCareuserMessagesConversa ...

The search for the "index" view in the views directory failed - Angular Universal SSR encounters errors with Firebase Cloud Functions

Currently, I am working through a tutorial on Server Side Rendering with Angular, Angular Universal, & Firebase 2021. The goal is to deploy my Angular universal project to Firebase hosting using Firebase functions. I managed to set up the emulator suc ...

How do I steer clear of this nested subscription when working with Angular Material's dialog box?

This is my current setup: this.entitiesManagementService.searchAddressEventEmitter$.pipe(switchMap((_) => { const formValue = this.addressAttributesSearchForm.getRawValue(); return forkJoin([ this.entitiesManagementService.getAddressClassificati ...

Mistakes following update to Angular 4 from Angular 2

After upgrading from Angular2 to Angular4, I encountered these errors in the CLI. While my app continues to function after the upgrade, I am curious about possible solutions to resolve these errors. Any suggestions? https://i.stack.imgur.com/CyYqw.png He ...

Issues have arisen with compiling TypeScript due to the absence of the 'tsc command not found' error

Attempting to set up TypeScript, I ran the command: npm install -g typescript After running tsc --version, it returned 'tsc command not found'. Despite trying various solutions from Stack Overflow, GitHub, and other sources, I am unable to reso ...

The local scope in Angular is inaccessible within an observable subscription

I've been grappling with this issue for quite some time and have experimented with numerous solutions. It seems that within my subscribe function, I'm unable to access ChangeDetectorRef or NgZone in order to trigger an update on the scope. Why ...

React Native Component Error: Cannot read property '_this' of undefined

I am currently developing a face recognition application using React Native 0.63. I'm running my project using react-native run-android. However, I encountered an issue with Component Exception where it shows 'undefined is not an object (evaluati ...

Revamping outdated dependencies in package.json for Angular 6

Currently, I am working with the latest version of Angular (6) and have been attempting to update my dependencies in the package.json. I was wondering if it is safe to use the npm update command to update all dependencies, or if there are other methods th ...

When attempting to send an archiver file in NodeJS, the request may become unresponsive

In my NextJS application, I am facing the challenge of generating a large number of QR codes at once, like 300, 400, or even 500, and then packaging them into a zip file for users to download. The following code snippet demonstrates how I achieve this usin ...

You must include the formControlName within a parent formGroup directive

Upon creating a model-driven form, an error is encountered: Error: formControlName must be used with a parent formGroup directive. You'll want to add a formGroup directive and pass it an existing FormGroup instance (you can create one in your class). ...

Using Material UI with React and TypeScript

I need some assistance with organizing my menus correctly in React using TypeScript. Currently, they are displaying on top of each other instead of within their respective category menus. I have been struggling to find a solution and would appreciate any h ...

The combination of Angular's *ngIf directive and ng-template is causing issues

When I have up to 3 icons, I require less space compared to when I have 3 icons or more. To address this issue, I have implemented the following code that utilizes both *ngIf and ng-template. Unfortunately, the implementation is not functioning as expect ...

What is the best approach for dynamically accessing or rendering Children within an Angular Component?

I am facing an issue with reusing a component called wrapper with different child components. I found some helpful resources such as this SO question and this article. However, these only address cases where the child component is known in advance. In my s ...

Is it possible to retrieve 2 arguments within a function in a non-sequential manner?

Let's say there is a function with arguments A, B, C, D, and E. Function(A, B, C, D, E) However, not all arguments are needed all the time. For instance, only A and C are needed in some cases. Currently, I would have to call the function like this: Fu ...