Archivo js (controller.js)
var app = angular.module("MyFirstApp",[]);
app.controller("FirstController",function($scope, $http){
$scope.posts = [];
$scope.newPost = {};
$http.get("http://jsonplaceholder.typicode.com/posts")
.success(function(data){
console.log(data);
$scope.posts = data;
})
.error(function(err){
});
$scope.addPost = function(){
$http.post("http://jsonplaceholder.typicode.com/posts",{
title: $scope.newPost.title,
body: $scope.newPost.body,
userId: 1
})
.success(function(data, status, headers, config){
$scope.posts.push($scope.newPost);
$scope.newPost = {};
})
.error(function(error,status, headers, config){
console.log(error);
});
}
});
Arhcivo HTML (index.html
"
Hola controller con Angular JS
Enviar Datos
{{ post.title}}
{{ post.body}}
"
Archivo js
angular.module("ToDoList",["LocalStorageModule"])
.controller("ToDoController", function($scope, localStorageService){
if (localStorageService.get("angular-todoList")) {
$scope.todo = localStorageService.get("angular-todoList");
}else {
$scope.todo = [];
}
$scope.$watchCollection("todo",function(newValue, oldValue){
localStorageService.set("angular-todoList",$scope.todo);
})
$scope.AddActividad = function(){
$scope.todo.push($scope.newActividad);
$scope.newActividad = {};
}
$scope.clearActividad = function(){
$scope.todo = [];
}
});
Archivo html
<!DOCTYPE html>
<html ng-app="ToDoList">
<head>
<meta charset="utf-8">
<title>Hola controller con Angular JS</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.32/angular.min.js"></script>
<script src="controller.js"></script>
<script src="angular-local-storage.min.js"></script>
</head>
<body ng-controller="ToDoController">
<ul>
<li ng-repeat="Actividad in todo">
{{Actividad.descripcion}} -
{{Actividad.fecha | date: 'short'}}
</li>
</ul>
<form ng-submit="AddActividad()">
<input type="text" ng-model="newActividad.descripcion">
<br><br>
<input type="datetime-local" ng-model="newActividad.fecha">
<br><br>
<input type="submit" value="Guardar actividad">
<button ng-click="clearActividad()">Limpiar</button>
</form>
</body>
</html>