Created
March 4, 2015 20:01
-
-
Save vic/1cd6b2620ed508ac430b to your computer and use it in GitHub Desktop.
Angular example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script> | |
<script src="script.js"></script> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
</head> | |
<body ng-app="stories" > | |
<h1>Hey {{name}} Your stories</h1> | |
<form ng-controller="StoriesCtrl"> | |
<textarea rows="3" cols="20" ng-model="newStory"> | |
</textarea> | |
<input type="button" value="Add" ng-click="createStory()"> | |
</form> | |
<tweets></tweets> | |
<script id="tweets-template" type="text/template"> | |
<div ng-repeat="story in stories"> | |
<div class="tweet"> | |
{{ story.content }} | |
</div> | |
</div> | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular | |
.module('stories', []) | |
.service('StoriesService', StoriesService) | |
.controller('StoriesCtrl', | |
StoriesController) | |
.directive('tweets', StoriesDirective) | |
; | |
function StoriesDirective () { | |
return { | |
restrict: 'E', | |
controller: 'StoriesCtrl', | |
template: document.getElementById('tweets-template').innerHTML | |
}; | |
} | |
function StoriesService ($http) { | |
var stories = [ | |
{ id: 1, author: 'Hugo', content: 'Hoy me desperte temprano'}, | |
{ id: 2, author: 'Paco', content: 'No era penal!' } | |
]; | |
this.all = function () { | |
$http.get('http://tec21-201861.usw1.nitrousbox.com/stories.json'). | |
then(function (response) { | |
console.log("From backend: ", response) | |
}); | |
return stories; | |
}; | |
this.nextId = function () { | |
return stories.length; | |
}; | |
this.create = function (story) { | |
stories.push(story); | |
}; | |
} | |
function StoriesController ($scope, StoriesService) { | |
$scope.name = "Dude"; | |
$scope.stories = StoriesService.all(); | |
$scope.newStory = ''; | |
$scope.createStory = function () { | |
var story = { | |
id: StoriesService.nextId(), | |
author: $scope.name, | |
content: $scope.newStory | |
}; | |
StoriesService.create(story); | |
$scope.stories = StoriesService.all(); | |
$scope.newStory = ''; | |
}; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment