angular4-testing/blog-complete/app/blog/app.component.ts

43 lines
908 B
TypeScript

import {Component} from '@angular/core';
import {initialEntries} from './initialEntries';
import {BlogEntry} from './blog-entry';
@Component({
moduleId: module.id,
selector: 'app-root',
templateUrl: 'app.component.html'
})
export class AppComponent {
entries: BlogEntry[] = [];
id = 0;
constructor() {
this.entries = [];
this.entries = initialEntries;
this.id = this.entries[this.entries.length-1].id;
}
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);
if (entryIndex > -1) {
this.entries.splice(entryIndex, 1);
}
}
}