-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.js
33 lines (31 loc) · 788 Bytes
/
http.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// modulos:
// * ngResource - API
var app = angular.module("MyFirstApp", []);
app.controller("FirstController", function($scope, $http){
$scope.posts = [];
$scope.loading = true;
$scope.newPost = {};
$http.get("http://jsonplaceholder.typicode.com/posts")
.success(function(data){
console.log(data);
$scope.posts = data;
$scope.loading = false;
})
.error(function(err){
$scope.loading = false;
});
$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(data);
$scope.newPost = {};
})
.error(function(error, status, headers, config){
console.log(error);
});
}
});