-1

I have a menu in an Ionic Angular application.

<ion-menu-toggle auto-hide="false" *ngFor="let p of menuItems; let i = index">
   <ion-item button (click)=p.method >
       <ion-icon slot="start" [ios]="p.icon + '-outline'" [md]="p.icon + '-sharp'"></ion-icon>
   </ion-item>
</ion-menu-toggle>
    public menuItems = [
    { title: 'New', method: this.newFile, icon: 'add' },
    { title: 'Open', method: this.openFile, icon: 'download' },
    { title: 'Save', method: this.saveFile, icon: 'save' },
    { title: 'Export', method: this.exportFile, icon: 'archive' }
    ]
    
    newFile(){ console.log("New");}
    openFile(){ console.log("Open");}
    ...

But the button click does nothing.

Is there a way to pass the method with the list or am i forced to have a method with a switch ?

   <ion-item button (click)=menuSwitch(p.method) >

    menuSwitch(item){
        if(item == 'New')
            NewFile();
        ...
    }

1 Answer 1

1

Of course you can do it. This is JS. You can do any thing. Just change your click like below

  <ion-menu-toggle auto-hide="false" *ngFor="let p of menuItems; let i = index">
    <ion-item button (click)="p.method()">
      <ion-icon slot="start" [ios]="p.icon + '-outline'" [md]="p.icon + '-sharp'"></ion-icon>
    </ion-item>
  </ion-menu-toggle> 
Sign up to request clarification or add additional context in comments.

4 Comments

It works great ! But visual studio still throws an error This expression is not callable. Not all constituents of type 'string | (() => void)' are callable. Type 'string' has no call signatures. (ngtsc(2349))
It works too but the error is still here.
are you sure it's not eslint error? just comment every thing in eslint file in project root and check if it still exists

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.