I have to pass a variable between angular controllers which are on different modules. Are services meant for this? Can anyone explain with an example?
Asked
Active
Viewed 1,049 times
1
-
Example code? What type of variable are you trying to pass? is it dynamic? or static? how often do you want to pass it? where does it come from? user input or database or...? – tennisgent Mar 06 '14 at 20:47
2 Answers
1
You will need to use providers rather than services. In a nutshell, injecting a service will provide a new instance of that service, whereas injecting a provider returns $get(), so you can coordinate shared variables.
There's a full example of what you're looking for here: sharing between modules with AngularJS?
0
Don't think about passing. Think about binding. Your controllers should be 'thin' and be focused on exposing your model to the view.
var myAppModule = angular.module('myApp', []);
myAppModule.factory("modelThingie", function() {
return {
stringimabob: "baz"
}
})
myAppModule.controller('AlphaCtrl', function($scope, modelThingie) {
$scope.model = modelThingie;
});
myAppModule.controller('BetaCtrl', function($scope, modelThingie) {
$scope.model = modelThingie;
});
In fiddle form (has view) :http://jsfiddle.net/willseitz/vmCb7/

WillSeitz
- 75
- 1
- 2