-
-
Save NatasaPeic/e8a010e0523d074b03aa1570c553b93b to your computer and use it in GitHub Desktop.
Service example with AngularJS for sharing scope data between controllers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment