Skip to content

Instantly share code, notes, and snippets.

@pmanijak
Created September 2, 2012 05:58
Show Gist options
  • Save pmanijak/3595195 to your computer and use it in GitHub Desktop.
Save pmanijak/3595195 to your computer and use it in GitHub Desktop.
Service and template example with AngularJS for sharing data between controllers
<!doctype html>
<html ng-app="project">
<head>
<title>Angular: Service and template example</title>
<script src="http://code.angularjs.org/angular-1.0.1.js"></script>
<script src="script.js"></script>
</head>
<body>
<div ng-view></div>
</body>
</html>
var projectModule = angular.module('project',[]);
projectModule.config(function($routeProvider) {
$routeProvider.
when('/', {controller:FirstCtrl, templateUrl:'x1.html'}).
when('/2', {controller:SecondCtrl, templateUrl:'x2.html'});
});
projectModule.factory('theService', function() {
return {
thing : {
x : 100
}
};
});
function FirstCtrl($scope, $location, theService) {
$scope.thing = theService.thing;
$scope.name = "First Controller";
}
function SecondCtrl($scope, theService) {
$scope.someThing = theService.thing;
$scope.name = "Second Controller!";
}
<div>
<h2>{{name}}</h2>
<input ng-model="thing.x"/>
<a href="#2">next</a>
</div>
<div>
<h2>{{name}}</h2>
<input ng-model="someThing.x"/>
<a href="#">back</a>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment