Angular: Empty Fiddle
http://angularjs.org/
HTML
<script src="http://code.angularjs.org/angular-1.0.1.js"></script>
<div ng-controller="MyCtrl">
Value:, {{test}}!
</div>
<div ng-controller="SetCtrl">
<input ng-model="newTestVal" placeholder="Enter value to set">
<button ng-click="setTestVal(newTestVal)">Set value</button>
</div>
JavaScript
var myApp = angular.module('myApp',[]);
myApp.factory('myService', function() {
var test = 5;
return{
setTestVal: function(val){
test = val;
},
getTestVal: function(){
return test;
}
}
});
function MyCtrl($scope, myService) {
$scope.test = myService.getTestVal();
}
function SetCtrl($scope, myService){
$scope.newTestVal = '';
$scope.setTestVal = function(val){
myService.setTestVal(val)
}
}