chandradot99
chandradot99

Reputation: 3876

How to call two functions on click in angularjs

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.

Upvotes: 5

Views: 11488

Answers (2)

Chris Hermut
Chris Hermut

Reputation: 1758

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).

Upvotes: 5

Tarun Dugar
Tarun Dugar

Reputation: 8971

Just call the functions separated by ;

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

Upvotes: 10

Related Questions