Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save KomanRudden/e5ff4ab5f786bcac5f20 to your computer and use it in GitHub Desktop.
Save KomanRudden/e5ff4ab5f786bcac5f20 to your computer and use it in GitHub Desktop.
AngularJS - share variables across controllers
Simple service example:
angular.module('myApp', [])
.service('sharedProperties', function () {
var property = 'First';
return {
getProperty: function () {
return property;
},
setProperty: function(value) {
property = value;
}
};
});
Using the service in a controller:
function Ctrl2($scope, sharedProperties) {
$scope.prop2 = "Second";
$scope.both = sharedProperties.getProperty() + $scope.prop2;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment