You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

app.component.ts 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import {Component} from '@angular/core';
  2. import {initialEntries} from './initialEntries';
  3. import {BlogEntry} from './blog-entry';
  4. import {FormBuilder, Validators} from '@angular/forms';
  5. @Component({
  6. selector: 'app-root',
  7. templateUrl: 'app.component.html'
  8. })
  9. export class AppComponent {
  10. title = 'Einkaufszettel';
  11. entries: BlogEntry[] = [];
  12. isSubmitted = false;
  13. Kategorien: any = ['Lebensmittel', 'Spielwaren', 'Technik', 'Sonstiges'];
  14. constructor(public fb: FormBuilder) {
  15. this.entries = [];
  16. this.entries = initialEntries;
  17. }
  18. // Getter method to access formcontrols
  19. get kategorieName() {
  20. return this.registrationForm.get('kategorieName');
  21. }
  22. /*########### Form ###########*/
  23. registrationForm = this.fb.group({
  24. kategorieName: ['', [Validators.required]]
  25. });
  26. saveTask(value: any){
  27. console.log(value);
  28. }
  29. createBlogEntry(title: string, image: string, kategorie: string, menge: string, kommentar: string, ) {
  30. if (title && image && kommentar && kategorie) {
  31. const entry = new BlogEntry();
  32. entry.title = title;
  33. entry.image = image;
  34. entry.kategorie = kategorie;
  35. entry.menge = menge;
  36. entry.kommentar = kommentar;
  37. this.entries.push(entry);
  38. }
  39. }
  40. deleteAllEntries() {
  41. console.log(this.entries.length);
  42. const index: number = this.entries.length;
  43. if (index !== -1) {
  44. this.entries = this.entries.splice(index, 1);
  45. }
  46. }
  47. deleteLastEntry() {
  48. console.log(this.entries.length);
  49. const index: number = this.entries.length;
  50. if (index !== -1) {
  51. this.entries.splice(index-1, 1);
  52. }
  53. }
  54. // Kategorie mit Drop Down Menu wählen
  55. changeKategorie(e) {
  56. console.log(e.value);
  57. this.kategorieName.setValue(e.target.value, {
  58. onlySelf: true
  59. });
  60. }
  61. /*########### Template Driven Form ###########*/
  62. onSubmit() {
  63. this.isSubmitted = true;
  64. if (!this.registrationForm.valid) {
  65. return false;
  66. } else {
  67. alert(JSON.stringify(this.registrationForm.value));
  68. }
  69. }
  70. }