Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call function into another function with in the same Controller Angular Js

I am new to using angularjs and i have declared a two functions in controller, and now i want to use one function into another function how can i do that means if i say function name into another function it says Undefined.

here is the code:

'use strict';
angular.module('customer').controller('Controller', ['$scope', '$state', 'Sservice',
  function($scope, $state, Sservice) {

    var that = this;

    (function getDetails() {
        //IMPLEMENTATION
    }());

    this.function2 = function function2 (id){
        //implementation
      getDetails(); // says undefined
    };
  }
]);
like image 990
Naresh k Avatar asked Jun 05 '15 09:06

Naresh k


People also ask

Can you call a function within the same function?

Calling a function from within itself is called recursion and the simple answer is, yes.

How do you call a method from one controller to another in AngularJS?

Communication between controllers is done though $emit + $on / $broadcast + $on methods. So in your case you want to call a method of Controller "One" inside Controller "Two", the correct way to do this is: app. controller('One', ['$scope', '$rootScope' function($scope) { $rootScope.

How do I call a function from another function in JavaScript?

To call a function inside another function, define the inner function inside the outer function and invoke it. When using the function keyword, the function gets hoisted to the top of the scope and can be called from anywhere inside of the outer function.


2 Answers

.controller('SampleController',function($scope){
$scope.funcA = function(){
   $scope.funcB();//scope level function
   funcC(); //non scope level function``
}
$scope.funcB = function(){
}
var funcC = function(){
}
});
like image 150
Rajkumar Rathi Avatar answered Oct 14 '22 18:10

Rajkumar Rathi


.controller('SampleController',function($scope){
$scope.funcA = function(){
   $scope.funcB();//scope level function
   funcC(); //non scope level function``
}
$scope.funcB = function(){
}
var funcC = function(){
}
});
like image 41
mohit sharma Avatar answered Oct 14 '22 20:10

mohit sharma