Vishwa G
Vishwa G

Reputation: 603

Angular ng-click multiple functions

<div ng-click="method1(); method2()> </div>

Here if the method1() calls the database / external service and gets the data. And method2() is some other behavior execution like UI bindings.
Suppose in case if there is a delay in getting the data from method1(), how to avoid or stop execution of method2() until method1() execution completes.

Note: I can't add method2() call from method1() due to some restrictions. Also I want to execute both these methods from the ng-click only.

Upvotes: 1

Views: 1054

Answers (3)

Hussain
Hussain

Reputation: 87

you can do something like this

function method1() {
    API.getData().then(function(response) {
        // call method2 here after you receive response
        method2()
    }, function(error) {});
}

html

<div ng-click="method1()> </div>

Upvotes: 1

samplename
samplename

Reputation: 9

Try with $timeout:

<div ng-click="method1()"></div>   
$scope.method1=function() {
    // calls the database / external service
    $timeout(function () {
        method2();
    }, 5000);
}

Upvotes: 1

Rakesh Chand
Rakesh Chand

Reputation: 3113

Call the method 2 in response of method1, like this

function method1() {
    service.getData().then(function(response) {
        //got the response here, do the operation and call method2 here
        method2()
    }, function() {});
}

If you call it giving any timeout, then it is not a sure solution. So call it in response.

Upvotes: 1

Related Questions