R
R
Roman Rakzin2015-07-02 01:51:41
Angular
Roman Rakzin, 2015-07-02 01:51:41

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

1 answer(s)
E
Evgeny Kumanin, 2015-07-02
@TwoRS

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 question

Ask a Question

731 491 924 answers to any question