Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Call a specific function which is defined in an Array of buttons from an HTML *ngFor statement

So,
I defined an array of button properties:

locationButtons: IButton[] = [
    {
      label: 'Customer-Hierarchy.LocationActions.SiteVisitButton',
      disabled: true,
      code: ButtonCode.SiteVisit,
      redirectUrl: 'this.onSiteVisitClick()'
    },
    {
      label: 'Customer-Hierarchy.LocationActions.ServiceInstallationButton',
      disabled: true,
      code: ButtonCode.ServiceInstallation,
      redirectUrl: 'this.onServiceInstallClick()'
    }
];

I want to call a function defined inside a redirect Url prop when the user clicks a button.

<div *ngFor="let item of locationButtons">
    <button ngbDropdownItem [disabled]=item.disabled
        (click)=item.redirectUrl>
        {{item.label | translate}}
    </button>
</div>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can assign the redirectUrl properties to the appropriate method:

locationButtons: IButton[] = [
    {
      ...
      redirectUrl: this.onSiteVisitClick
    },
    {
      ...
      redirectUrl: this.onServiceInstallClick
    }
];

An then bind the click event to the method call.

<div *ngFor="let item of locationButtons">
    <button ngbDropdownItem [disabled]="item.disabled"
        (click)="item.redirectUrl()">
        {{item.label | translate}}
    </button>
</div>

cheers

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading