简体   繁体   中英

Rename function's name inside button angular 9

On change select's function... Depending on the selected option, I want to rename the 'functionX' dynamically... Frontend

<select [(ngModel)]="selectedOp" (change)="selectedOption()">
   <option value={{op}} *ngFor="let op of Options">{{op}}</option>>
</select>

<button id="btnID" type="submit" (click)="functionX()">
 Button
</button>

Backend

I've been trying this using js...

Options = ['Function1', 'Function2'];

selectedOp;

selectedOption(){
    switch (this.selectedOp) {
      case 'Function1':
          const btn = document.getElementById('btnID');
          btn.setAttribute('(click)', 'function1()');
      break;
      case 'Function2':
          const btn = document.getElementById('btnID');
          btn.setAttribute('(click)', 'function2()');
      break
}

Then this error happens...

ERROR DOMException: Failed to execute 'setAttribute' on 'Element': '(click)' is not a valid attribute name..

If you guys know of a better way to do this, I'd appreciate your sharing :)

selectedOption(){
switch (this.selectedOp) {
  case 'Function1':
      this.funciton1();
  break;
  case 'Function2':
      this.function2();
  break
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM