angular4-testing/project-manager/src/app/blog/blog.component.ts

47 lines
1.1 KiB
TypeScript
Raw Normal View History

2017-03-02 23:26:26 +01:00
import {Component} from '@angular/core';
2017-03-14 04:47:27 +01:00
import {ActivatedRoute, Router} from '@angular/router';
2017-03-02 23:26:26 +01:00
import {Title} from '@angular/platform-browser';
import {initialEntries} from './initialEntries';
2017-03-03 09:05:29 +01:00
import {BlogEntry} from './blog-entry/blog-entry';
2017-03-02 23:26:26 +01:00
@Component({
selector: 'blog',
templateUrl: 'blog.component.html',
styleUrls: ['blog.component.css']
})
export class BlogComponent {
2017-03-14 04:47:27 +01:00
entries: BlogEntry[];
id: number = 0;
2017-03-02 23:26:26 +01:00
constructor(r: ActivatedRoute, private router: Router, private titleService: Title) {
this.entries = initialEntries;
2017-03-14 04:47:27 +01:00
if (this.entries.length != 0) {
this.id = this.entries[this.entries.length-1].id;
}
2017-03-02 23:26:26 +01:00
}
createBlogEntry(title:string, image:string, text:string) {
this.id++;
console.log(this.id, title, image, text);
let entry = new BlogEntry();
entry.id = this.id;
entry.title = title;
entry.image = image;
entry.text = text;
this.entries.push(entry);
}
deleteBlogEntry(id:number) {
let entryIndex = this.entries.findIndex(entry => entry.id === id);
2017-03-14 04:47:27 +01:00
if (entryIndex >= 0) {
2017-03-02 23:26:26 +01:00
this.entries.splice(entryIndex, 1);
}
}
}