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 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. constructor(public fb: FormBuilder) {
  11. this.entries = [];
  12. this.entries = initialEntries;
  13. }
  14. // Getter method to access formcontrols
  15. get kategorieName() {
  16. return this.registrationForm.get('kategorieName');
  17. }
  18. title = 'Einkaufszettel';
  19. entries: BlogEntry[] = [];
  20. isSubmitted = false;
  21. // Kategorien
  22. Kategorien: any = ['Lebensmittel', 'Spielwaren', 'Technik', 'Sonstiges'];
  23. /*########### Form ###########*/
  24. registrationForm = this.fb.group({
  25. kategorieName: ['', [Validators.required]]
  26. });
  27. saveTask(value: any){
  28. console.log(value);
  29. }
  30. createBlogEntry(title: string, image: string, kategorie: string, menge: string, kommentar: string, ) {
  31. if (title && image && kommentar && kategorie) {
  32. const entry = new BlogEntry();
  33. entry.title = title;
  34. entry.image = image;
  35. entry.kategorie = kategorie;
  36. entry.menge = menge;
  37. entry.kommentar = kommentar;
  38. this.entries.push(entry);
  39. }
  40. }
  41. // Kategorie mit Drop Down Menu wählen
  42. changeKategorie(e) {
  43. console.log(e.value);
  44. this.kategorieName.setValue(e.target.value, {
  45. onlySelf: true
  46. });
  47. }
  48. /*########### Template Driven Form ###########*/
  49. onSubmit() {
  50. this.isSubmitted = true;
  51. if (!this.registrationForm.valid) {
  52. return false;
  53. } else {
  54. alert(JSON.stringify(this.registrationForm.value));
  55. }
  56. }
  57. }