Guest User

Untitled

a guest
Mar 23rd, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. export class PostService {
  2.  
  3. posts = [
  4. {
  5. title: "Post 1",
  6. content: "bla bla bla",
  7. loveIts: 0,
  8. created_at: new Date,
  9. },
  10. {
  11. title: "Post 2",
  12. content: "bla bla bla",
  13. loveIts: 0,
  14. created_at: new Date,
  15. },
  16. {
  17. title: "Post 3",
  18. content: "bla bla bla",
  19. loveIts: 0,
  20. created_at: new Date,
  21. },
  22. ];
  23.  
  24. getPosts() {
  25. return this.posts;
  26. }
  27.  
  28. setLoveItsToDefault() {
  29. for (let post of this.posts) {
  30. post.loveIts = 0;
  31. }
  32. }
  33.  
  34. initializeLovesPost(i: number) {
  35. this.posts[i].loveIts = 0;
  36. }
  37. }
  38.  
  39. export class AppComponent {
  40.  
  41. @Input() posts : Post[];
  42.  
  43. constructor(private postService: PostService) {
  44. }
  45.  
  46. ngOnInit() {
  47. this.posts = this.postService.getPosts();
  48. }
  49.  
  50. onInitializeLoves() {
  51. if (confirm('Are you sure to initialize all loves of all posts to 0 ?')) {
  52. this.postService.setLoveItsToDefault();
  53. } else {
  54. return null;
  55. }
  56. }
  57. }
  58.  
  59. <button class="btn btn-primary" (click)="onInitializeLoves()">Initialize loves to 0</button>
Add Comment
Please, Sign In to add comment