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
};
}
]);
Calling a function from within itself is called recursion and the simple answer is, yes.
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.
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.
.controller('SampleController',function($scope){
$scope.funcA = function(){
$scope.funcB();//scope level function
funcC(); //non scope level function``
}
$scope.funcB = function(){
}
var funcC = function(){
}
});
.controller('SampleController',function($scope){
$scope.funcA = function(){
$scope.funcB();//scope level function
funcC(); //non scope level function``
}
$scope.funcB = function(){
}
var funcC = function(){
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With