Guest User

Untitled

a guest
Jun 25th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. import { PostService } from './../post.service';
  2. import { Component, OnInit } from '@angular/core';
  3.  
  4. @Component({
  5. selector: 'app-post',
  6. templateUrl: './post.component.html',
  7. styleUrls: ['./post.component.css']
  8. })
  9. export class PostComponent implements OnInit {
  10.  
  11. posts: any;
  12.  
  13. constructor(private postService:PostService) { }
  14.  
  15. ngOnInit() {
  16. this.getPosts();
  17. }
  18.  
  19. getPosts() {
  20.  
  21. this.postService.getPosts().subscribe(
  22. data => {
  23.  
  24. this.posts = data;
  25.  
  26. console.log(this.posts);
  27. }
  28. );
  29.  
  30. }
  31.  
  32. createPost() {
  33.  
  34. //set to publish
  35. this.formData.status = 'publish';
  36.  
  37. console.log(this.formData);
  38.  
  39. this.postService.createPost(this.formData).subscribe(
  40. data => {
  41. console.log(data);
  42.  
  43. //redirect to post list
  44.  
  45. this.router.navigate(['post']);
  46. },
  47. error => {
  48.  
  49. }
  50. );
  51.  
  52. }
  53.  
  54. deletePost(id) {
  55.  
  56. let choice = confirm('Are you sure to delete?');
  57.  
  58. if (choice) {
  59.  
  60. console.log(id);
  61.  
  62. this.postService.deletePost(id).subscribe(
  63. data => {
  64. this.getPosts();
  65. }
  66. );
  67.  
  68. }
  69.  
  70. }
  71.  
  72. }
Add Comment
Please, Sign In to add comment