Ionic/Angular - Run method with name from html

Foxhunt

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();
        ...
    }
armin momeni

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> 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to run a java method on click from html?

From Dev

How to run jar file or java method from an html page?

From Dev

Run a method in an instance/object whose name is dependent on an input from another instance's method

From Dev

remove the ? and name= in get method html

From Dev

Run method on class knowing only its name

From Dev

Change thread name used to run @StreamListener method

From Dev

Run async method from Xaml

From Dev

Run method from class in for loop

From Dev

Run method from different scope

From Java

Calling method of Another class from run() method

From Dev

calling python method name from name

From Java

Method name from line number

From Dev

Implicit Verbs from Method name

From Dev

Get method name from within a typescript method

From Dev

Invoking a method from a string method name

From Dev

Calling a method in ruby from a method with the same name

From Dev

How to run tests on a single method when the method name `is backticked`()

From Dev

run method from actionbar icon from mainactivity

From Dev

Run coffeescript method on html input button click

From Dev

jquery run instruction from html

From Dev

Run script from html button

From Dev

Run function from a package by reflecting its name

From Dev

Call a method that returns html from an action method

From Dev

Getting method to run in the background from remote notification

From Dev

Run Overriden Method Automatically From Base Class

From Dev

SBT Run Main Method from a Sub Project

From Dev

Run method from loaded class file?

From Dev

Run method from ViewController A on ViewController B

From Dev

Run method from controller class on startup Javafx

Related Related

HotTag

Archive