Answer the question
In order to leave comments, you need to log in
How to pass data between controllers in angularjs?
There are several controllers in Angular.
How can I access the value/function of another from rootScop or from one controller?
- - - -
myApp.controller('AdminController', function($scope,$http,$rootScope) {
$scope.get_data= function () {
alert("I'm from AdminController");
}
$scope.get_data_in_SettingsController= function ( ) {
//how do I refer to SettingsController here?
}
}
myApp.controller('SettingsController', function($scope,$http,$rootScope) {
$scope.get_data= function () {
alert("I'm from SettingsController") ;
}
}
Answer the question
In order to leave comments, you need to log in
Use factories.
var myApp = angular.module('myApp', []);
// make a factory to share data between controllers
myApp.factory('Data', function(){
var data = {
firstName: ''
};
return {
getFirstName: function () {
return data.firstName;
},
setFirstName: function (firstName) {
data.firstName = firstName;
}
};
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question