GeForce RTX 4090
GeForce RTX 4090

Reputation: 3498

Angular: bind event based on expression

Which function should execute when form is submitted is dependent on whether it is edit mode or not. Is it possible to do something like this in Angular (the code below just throws an error):

<form novalidate (ngSubmit)="{editMode ? saveUser() : addUser()}" [formGroup]="userForm">

Upvotes: 0

Views: 44

Answers (3)

Pardeep Jain
Pardeep Jain

Reputation: 86740

You can do like this -

Just remove {} sign from the event call like this -

<form novalidate (ngSubmit)="editMode ? saveUser() : addUser()" [formGroup]="userForm">

Working example

Upvotes: 2

Sameer
Sameer

Reputation: 519

try this

   <form novalidate (ngSubmit)="editMode ? saveUser() : addUser()" [formGroup]="userForm">

Upvotes: 1

Arthur Groupp
Arthur Groupp

Reputation: 1

No, you can't. But what you can do is:

<form novalidate (ngSubmit)="saveUser(editMode ? 'add' : 'save')"[formGroup]="userForm">

saveUser(mode: 'add' | 'save') {
   switch(mode) { .... }
}

Upvotes: 0

Related Questions