Skip to content

Instantly share code, notes, and snippets.

@pmanijak
Created September 2, 2012 06:42
Show Gist options
  • Star 29 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save pmanijak/3595424 to your computer and use it in GitHub Desktop.
Save pmanijak/3595424 to your computer and use it in GitHub Desktop.
Service example with AngularJS for sharing scope data between controllers
<!doctype html>
<html ng-app="project">
<head>
<title>Angular: Service example</title>
<script src="http://code.angularjs.org/angular-1.0.1.js"></script>
<script>
var projectModule = angular.module('project',[]);
projectModule.factory('theService', function() {
return {
thing : {
x : 100
}
};
});
function FirstCtrl($scope, theService) {
$scope.thing = theService.thing;
$scope.name = "First Controller";
}
function SecondCtrl($scope, theService) {
$scope.someThing = theService.thing;
$scope.name = "Second Controller!";
}
</script>
</head>
<body>
<div ng-controller="FirstCtrl">
<h2>{{name}}</h2>
<input ng-model="thing.x"/>
</div>
<div ng-controller="SecondCtrl">
<h2>{{name}}</h2>
<input ng-model="someThing.x"/>
</div>
</body>
</html>
@bennypowers
Copy link

Would this work when you have controllerAs syntax and don't have implicit inheritance?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment