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}}! <br>
New Value : {{data.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;
var obj = {
test : 5
}
return{
setTestVal: function(val){
test = val;
obj.test = val;
},
getTestVal: function(){
return test;
},
data : obj
}
});
function MyCtrl($scope, myService) {
$scope.test = myService.getTestVal();
$scope.data = myService.data;
}
function SetCtrl($scope, myService){
$scope.newTestVal = '';
$scope.setTestVal = function(val){
myService.setTestVal(val)
}
}