Skip to content

Instantly share code, notes, and snippets.

@wpflames
Created June 28, 2022 11:04
Show Gist options
  • Select an option

  • Save wpflames/730da1ab752f29222bc77e3817bf412f to your computer and use it in GitHub Desktop.

Select an option

Save wpflames/730da1ab752f29222bc77e3817bf412f to your computer and use it in GitHub Desktop.
Create a new post object with form and display it in the console
import { HttpClient } from '@angular/common/http';
import { Component } from '@angular/core';
@Component({
selector: 'posts',
templateUrl: './posts.component.html',
styleUrls: ['./posts.component.scss']
})
export class PostsComponent {
posts: any;
private url = 'https://jsonplaceholder.typicode.com/posts';
constructor(private http: HttpClient) {
http.get(this.url).subscribe(res => {
console.log(res);
this.posts = res;
});
}
createPost(input: HTMLInputElement){
let post = { title: input.value };
this.http.post(this.url, JSON.stringify(post))
.subscribe(res => {
console.log(res);
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment