Skip to content

Instantly share code, notes, and snippets.

@lvidarte
Forked from anonymous/index.html
Last active December 18, 2015 21:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lvidarte/c9406e8a772d7a858d2b to your computer and use it in GitHub Desktop.
Save lvidarte/c9406e8a772d7a858d2b to your computer and use it in GitHub Desktop.
Angular providers
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.min.js"></script>
<meta charset="utf-8">
</head>
<body ng-app="app">
<div ng-controller="ctrl">
Score: {{ score.points }}
<button ng-click="increment()">Increment</button>
</div>
<script>
angular
.module('app', [])
.value('randomScore', function() {
return Math.ceil(Math.random() * 10);
})
.factory('score', function(randomScore) {
return {points: randomScore()};
})
.controller('ctrl', function($scope, score) {
$scope.score = score;
$scope.increment = function() {
$scope.score.points++;
};
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment