1

<div id="home" class="tab-pane fade in" style="text-align:left;opacity: 1;" >
   
  <button *ngFor="let tool of toolArray" class="m-btn btn btn-secondary" type="button" (click)="{{tool.ToolMethod}}()" placement="bottom"
      ngbTooltip="{{tool.Tooltip}}">
      <img src={{tool.ToolImgPath}} alt="" width="24" height="24"/>
  </button>

</div>

Lets say , I have some tools managed by admin , if the user logs in , he can use that tools for editing .

 let toolArray = [
    {ToolCategory: "analysis"
    ToolId: 96
    ToolImgPath: "images/zoom-selection.png"
    ToolMethod: "zoomToClickedFeature"
    ToolName: "an_zoomto_selected"
    Tooltip: "Zoom To Selected Feature"}
]

if I add this in the html using ngFor , its getting error like

Got interpolation ({{}}) where expression was expected

2 Answers 2

3

If the method is a member of the controller, you should use this like: (click)="this[tool.ToolMethod]()".

Here's a working example:

https://stackblitz.com/edit/angular-q2l54d?file=src%2Fapp%2Fapp.component.html

Sign up to request clarification or add additional context in comments.

2 Comments

but how to handle error if the tool Method is not present in the component ? getting this error ERROR TypeError: _co[_v.context.$implicit.ToolMethod] is not a function ,if we forget to define the method
You could check for undefined before calling (click)="this[item.method]&&this[item.method]()"
2

Remove the curly brackets from (click)="{{tool.ToolMethod}}()" to (click)="tool.ToolMethod()" and refactor your code to something like this, to get the reference to the corresponding method right:

public zoomToClickedFeature() {
  console.log("do something");
}

public toolArray = [
  {
    ToolCategory: "analysis",
    ToolId: 96,
    ToolImgPath: "images/zoom-selection.png",
    ToolMethod: this.zoomToClickedFeature,
    ToolName: "an_zoomto_selected",
    Tooltip: "Zoom To Selected Feature"
  }
];

Array and function must be class properties of the component.

Comments

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.