Skip to content

Instantly share code, notes, and snippets.

@vic
Last active August 29, 2015 14:15
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 vic/6e14f1419fc82cab6bb4 to your computer and use it in GitHub Desktop.
Save vic/6e14f1419fc82cab6bb4 to your computer and use it in GitHub Desktop.
Patos as a service
Patos demo
How to serve:
`python -m SimpleHTTPServer`
open http://localhost:8000
<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
<script src="index.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body ng-app="patito">
<br />
<div ng-controller="MainCtrl">
{{ hola }} {{ nombre }}
<label>
Vuela vuela?
<input type="checkbox" ng-model="volador"
ng-true-value="true"
ng-false-value="false" />
</label>
<label>Nombre</label>
<input type="text" ng-model="nombre" />
<div ng-if="volador">
Esta madre vuela
</div>
<div ng-if="!volador">
Esta madre se arrastra
</div>
<div ng-controller="PatoEditorCtrl">
<h3>Crea un Pato</h3>
<input type="text" ng-model="pato.nombre" />
<input type="number" ng-model="pato.edad" min=1 max=20 />
<input type="checkbox" ng-model="pato.vuela"
ng-true-value="true"
ng-false-value="false" />
<input type="button" value="Crear Pato" ng-click="save()" />
</div>
<div>
<h3>Tus patos</h3>
<ul>
<li ng-repeat="pato in patoList()">
<pato-show pato="pato" />
</li>
</ul>
</div>
</div>
Hola
<script type="text/template" id="pato-template">
<i>Hola yo soy un pato {{ pato.nombre }} </i>
</script>
</body>
</html>
angular.
module("patito", []).
service('PatosService', PatosService).
directive('patoShow', PatoElement).
controller('PatoEditorCtrl', PatoEditorCtrl).
controller('PatoCtrl', PatoCtrl).
controller('MainCtrl', MainCtrl)
;
MainCtrl.$inject = ['$scope', 'PatosService'];
function MainCtrl ($scope, bolsaDePatos) {
$scope.hola = "Hello world";
$scope.nombre = "Luis"
$scope.volador = true;
$scope.patoList = bolsaDePatos.findAll;
}
function PatoEditorCtrl ($scope, PatosService) {
var patoGenerico = {
nombre: "Hugo",
edad: 10,
vuela: true
};
$scope.pato = Object.create(patoGenerico);
$scope.save = function () {
console.log("Creando pato ", $scope.pato);
PatosService.create($scope.pato);
$scope.pato = Object.create(patoGenerico);
}
}
function PatosService () {
var patos = [];
this.findAll = function () {
return patos;
}
this.create = function (pato) {
patos.push(pato);
}
}
function PatoCtrl ($scope) {
}
function PatoElement () {
return {
restrict: 'E',
scope: {
pato: '='
},
controller: 'PatoCtrl',
template: document.getElementById('pato-template').innerHTML
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment