Calling a function into another function using the same Angular Js controller

I'm new to using angularjs and I declared two functions in the controller, and now I want to use one function in another function, how can I do this means that if I say the name of the function in 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 }; } ]); 
+6
source share
8 answers

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

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> 
+3
source

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

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

 function getDetails() { //IMPLEMENTATION } this.function2 = function(id) { getDetails(); }; 
0
source

You make things complicated. Just do it 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 }; } ]); 
0
source

In the above example, several code problems are running. To start, function2 not declared properly.

You wrapped your getDetails function in a so-called self - signed anonymous function . This means that it is not displayed for code outside the SEAF shell, including function2 . Omit the SEAF shell, so getDetails is determined when function2 wants to use it.

Finally, you use Angular, but assigning function2 - this on the controller. This is probably not what you wanted to do; the functions you want to open in HTML must be bound 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(); }; } ]); 
0
source

 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 }; } ]); 
0
source

Work great 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(); } } } } 
0
source

Source: https://habr.com/ru/post/988565/


All Articles