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

43 lines
908 B
TypeScript
Raw Permalink 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-28 20:12:40 +01:00
id = 0;
2017-02-27 01:44:23 +01:00
constructor() {
this.entries = [];
2017-02-28 20:12:40 +01:00
this.entries = initialEntries;
this.id = this.entries[this.entries.length-1].id;
2017-02-27 01:44:23 +01:00
}
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
}