Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 435
Tom Cheng Avatar asked Jul 18 '13 08:07

Tom Cheng


People also ask

How do you call a function in a scope?

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

Can we call function from another controller in AngularJS?

If the two controller is nested in One controller. Then you can simply call: $scope. parentmethod();

What is $scope in AngularJS?

The $scope in an AngularJS is a built-in object, which contains application data and methods. You can create properties to a $scope object inside a controller function and assign a value or function to it. The $scope is glue between a controller and view (HTML).


2 Answers

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

like image 134
Yann Avatar answered Oct 07 '22 02:10

Yann


$scope.fn1 = function() {     // do something A };  $scope.fn2 = function() {     // do something B     // I want to call fn1 here.      $scope.fn1(); // this works  }; 
like image 41
Edward Avatar answered Oct 07 '22 02:10

Edward