Angular: Empty Fiddle
http://angularjs.org/
by Alejandro M
HTML
<script src="http://code.angularjs.org/angular-1.0.1.js"></script>
<script src="https://code.angularjs.org/1.2.6/angular.min.js"></script>
<div ng-controller="MyCtrl">
Value: {{test.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 obj = {
test : 5
}
return{
setTestVal: function(val){
obj.test = val;
},
getTestVal: function(){
return obj;
}
}
});
function MyCtrl($scope, myService) {
$scope.test = myService.getTestVal();
}
function SetCtrl($scope, myService){
$scope.newTestVal = '';
$scope.setTestVal = function(val){
myService.setTestVal(val)
}
}