5

I have two functions inside my controller

$scope.first = function()
{
    console.log("first");
}

$scope.second = function()
{
console.log("hello");
}

Now in my template i give call to first function

<button ng-click="first()">click</button>

Now I want that as soon as my first function complete its execution, automatically my second function starts executing without any click.

That is on single click first function executes and then second function executes respectively.

2 Answers 2

10

Just call the functions separated by ;

<button ng-click="first();second()">click</button>
Sign up to request clarification or add additional context in comments.

Comments

5

Or you can do:

function first() {
    // Do stuff
}

function second() {
   // Do stuff
}

$scope.runBoth = function () {
    first();
    second();
}

The good thing with that approach is that you can listen to first() return and based on that run/skip execution of second() etc.

Also by keeping first() and second() as just javascript functions you are not adding unnecessary watchers (assuming that you're not using first() and second() somewhere else).

2 Comments

of corse this will run ok, but it's easier to call both function in the ng-click
It all depends on a use case.

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.