Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { PostService } from './../post.service';
- import { Component, OnInit } from '@angular/core';
- @Component({
- selector: 'app-post',
- templateUrl: './post.component.html',
- styleUrls: ['./post.component.css']
- })
- export class PostComponent implements OnInit {
- posts: any;
- constructor(private postService:PostService) { }
- ngOnInit() {
- this.getPosts();
- }
- getPosts() {
- this.postService.getPosts().subscribe(
- data => {
- this.posts = data;
- console.log(this.posts);
- }
- );
- }
- createPost() {
- //set to publish
- this.formData.status = 'publish';
- console.log(this.formData);
- this.postService.createPost(this.formData).subscribe(
- data => {
- console.log(data);
- //redirect to post list
- this.router.navigate(['post']);
- },
- error => {
- }
- );
- }
- deletePost(id) {
- let choice = confirm('Are you sure to delete?');
- if (choice) {
- console.log(id);
- this.postService.deletePost(id).subscribe(
- data => {
- this.getPosts();
- }
- );
- }
- }
- }
Add Comment
Please, Sign In to add comment