49

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!

3 Answers 3

101

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

Sign up to request clarification or add additional context in comments.

6 Comments

Fn1() must be declared before call it on fn2()
@RolandoCC Thanks alot. I was so sure how to call it but this have to declare before was killing me -*-
@RolandoCC how to declare it ??
@Melvin just : $scope.fn1 = function(){ };
I have the same issue. fn1() just isn't being called and I don't understand why.
|
3
$scope.fn1 = function() {
    // do something A
};

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

    $scope.fn1(); // this works

};

Comments

-4

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.

1 Comment

I think you should show the user how to do what he asks. He may use fn1 not only from fn2, but from anywhere else

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.