16

i am using angular 5 and facing a problem. i want to submit (click) event and call two or multiple method one bye one. Please give me idea or solution so that i can submit a (click) event and call two or multiple method.

such as

.html file

   <button (click)="abc(); bcde()"></button>

.ts file

  first method  
  abc(){}

  second method method
  bcde(){}

is this a right way to do this. Please suggest me and help me to solve this

4
  • What issue are you facing ? Whats the error ? Commented Jan 18, 2018 at 9:17
  • 1
    You can call other methods from abd() like this abc(){ this.bcde();this.cdef();} Commented Jan 18, 2018 at 9:17
  • can you please tell me the correct way to call multiple method.. i am new in angular 5 Commented Jan 18, 2018 at 9:30
  • I had this problem on rest api calls, they would not call in order till i found this article: stackoverflow.com/a/45737338/1178375 Commented Aug 20, 2021 at 20:42

3 Answers 3

40

you can also do

<button (click)="[abc(), bcde()]"></button>
Sign up to request clarification or add additional context in comments.

Comments

12
<button (click)="callall()"></button>

make one function and call all the function in that

function 1

abc(){}

function 2

bcde(){}

call both in common function

 callall(){

        this.abc()
        this.bcde() 
}

3 Comments

It's a use case that is best avoided raihan, as the others have already pointed out. Better to have a single method you call and from there call the other methods you want.
why should this use case be avoided? It can happen that a click triggers two differ actions and it's not bad to make that explicit.
I agree that this is a bad practice! They should be called separately too avoid multiple code smells and pit falls.
9

This is the correct way I guess. It is working for me.

<button (click)="abc(); bcde()"></button>

2 Comments

I tried this approach in AngularDart and it did not work. I had to make a new function that calls the other two.
oo waw that is a good input, good to know @Pi

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.