Tom Cheng
Tom Cheng

Reputation: 1389

How to call another scope function in AngularJS

In AngularJS, I have 2 scope function in the controller,

$scope.fn1 = function(){
//do something A
};

$scope.fn2 = function(){
    //do something B
    //I want to call fn1 here.
};

If my fn2 want to call fn1, how can I do? Thanks!

Upvotes: 49

Views: 71575

Answers (3)

Edward
Edward

Reputation: 55

$scope.fn1 = function() {
    // do something A
};

$scope.fn2 = function() {
    // do something B
    // I want to call fn1 here.

    $scope.fn1(); // this works

};

Upvotes: 3

Janaka Sampath
Janaka Sampath

Reputation: 85

if you are doing same thing inside both fn1 and fn2 you don't want two scope function, use one function for that.

if you need to addition in fn2 then take common thing for out of two functions then call that.

Upvotes: -4

Yann
Yann

Reputation: 2221

Since both functions are in the same scope you can simply call $scope.fn1() inside fn2.

Upvotes: 101

Related Questions