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

42 lines
853 B
TypeScript
Raw Normal View History

2017-02-27 01:44:23 +01:00
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'
})
2017-02-27 11:45:59 +01:00
2017-02-27 01:44:23 +01:00
export class AppComponent {
entries: BlogEntry[] = [];
2017-02-27 11:45:59 +01:00
id = 2;
2017-02-27 01:44:23 +01:00
constructor() {
this.entries = [];
this.entries = initialEntries
}
createBlogEntry(title:string, image:string, text:string) {
2017-02-27 11:45:59 +01:00
this.id++;
console.log(this.id, title, image, text);
2017-02-27 01:44:23 +01:00
let entry = new BlogEntry();
2017-02-27 11:45:59 +01:00
entry.id = this.id;
2017-02-27 01:44:23 +01:00
entry.title = title;
entry.image = image;
entry.text = text;
this.entries.push(entry);
}
2017-02-27 11:45:59 +01:00
deleteBlogEntry(id:number) {
let entryIndex = this.entries.findIndex(entry => entry.id === id);
if (entryIndex > -1) {
this.entries.splice(entryIndex, 1);
}
}
2017-02-27 01:44:23 +01:00
}