9

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
    };
  }
]);
3
  • 1
    i just wan to know how to call a function inside another function in the same controller Commented Jun 5, 2015 at 9:56
  • function a() { b(); }. Commented Jun 5, 2015 at 9:57
  • i have added code to my question can you check and please tell me where i went wrong? Commented Jun 5, 2015 at 10:04

8 Answers 8

13

.controller('SampleController',function($scope){
$scope.funcA = function(){
   $scope.funcB();//scope level function
   funcC(); //non scope level function``
}
$scope.funcB = function(){
}
var funcC = function(){
}
});

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

2 Comments

but my functions are not in $scope
Can you share the controller structure
3

Worked best for me

var app = angular.module('MyApp', []);
app.controller('MyCtrl',['$scope',function($scope)
 {
$scope.functionA=function(){
alert("Inside functionA")
$scope.functionB(); 	
}; 								 
$scope.functionB=function(){ 	 									
alert("Inside functionB");	 
}
}]);
<!DOCTYPE html>
<html ng-app="MyApp" ng-controller="MyCtrl">
  
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.min.js"></script>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>


<input type="button" value="Click to call functionA" ng-click="functionA()">


</body>
</html>

Comments

2

.controller('SampleController',function($scope){
$scope.funcA = function(){
   $scope.funcB();//scope level function
   funcC(); //non scope level function``
}
$scope.funcB = function(){
}
var funcC = function(){
}
});

Comments

0

I don't know what you're trying to achieve exactly, but you can simply declare your two functions as

function getDetails() {
    //IMPLEMENTATION
}

this.function2 = function(id) {
    getDetails(); 
};

2 Comments

getDetails is global function it will execute when controller is loaded function2 is executes when user clicks on button i want to execute getDetails function when user clicks on button i.e. i want to call getDetails in function2 i tried the same logic which you pasted above but it says undefined
No, getDetail() is not a global function. It's only visible from the controller function. And function2() will call it if you declare it as I did in my answer. Post the updated code in your question, post all the other relevant code, and post the complete and exact error message you get.
0

You are making things complex. Simply, do like this

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

        function getDetails() {
            //IMPLEMENTATION
        };

        function function2 (id){
            //implementation
          getDetails(); // says undefined
        };
      }
]);

3 Comments

you are not understanding what i am asking for getDetails is global function it will execute when controller is loaded function2 is executes when user clicks on button i want to execute getDetails function when user clicks on button i.e. i want to call getDetails in function2
getDetails will be declared (not globally, privately in the controller) and will not be called until you invoke function2.
If my understanding is correct - You can to call getDetails() at the time of execution of controller and also when function2 is called. Please confirm
0

Several areas of code are confused in your example above. For a start, function2 is not declared properly.

You've wrapped your getDetails function into what is known as a self-executing anonymous function. This means it is not visible to code outside the SEAF wrapper, including function2. Omit the SEAF wrapper so getDetails is defined when function2 wants to use it.

Finally, you are using Angular but assigning function2 to this on the controller. This is probably not what you wanted to do; functions that you want to expose to the HTML should be attached to $scope, not this.

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

    function getDetails() {
        //IMPLEMENTATION
    }

    $scope.function2 = function(id) {
        //implementation
        getDetails();
    };
  }
]);

Comments

0

My these options below could help

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

    function getDetails() {
        //IMPLEMENTATION
    };

    function function2 (id){
        //implementation
      getDetails(); // says undefined
    };
  }
]);


or 

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

    $scope.getDetails = function() {
        //IMPLEMENTATION
    };

    $scope.function2 = function(id){
        //implementation
      $scope.getDetails(); // says undefined
    };
  }
]);

Comments

0

Work fine for me:

{
    // define angular module/app

    var formApp = angular.module('formApp', []);

    // create angular controller and pass in $scope and $http
    function formController($scope, $http) {

        $scope.sitelist = function(){
            $http.get("http://mars.ourgoogle.in/clients/techinfini/customcms/index.php/Ajax/sitelist").then(function(items){    
            console.log(items.data);
            $scope.list = items.data;       
        }); 
    }

    // process the form
    $scope.processForm = function() {
        $http({
            method  : 'POST',
            url     : 'http://mars.ourgoogle.in/clients/techinfini/customcms/index.php/Ajax/angulartest',
            data    : $.param($scope.formData),  // pass in data as strings
            headers : { 'Content-Type': 'application/x-www-form-urlencoded' }  // set the headers so angular passing info as form data (not request payload)
        }).success(function(data) {
                $scope.sitelist();
           }
        }
    }
}

1 Comment

cannot find name '$scope'

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.