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.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. createBlogEntry(title: string, image: string, kategorie: string, text: string, ) {
  28. if (title && image && text && kategorie) {
  29. const entry = new BlogEntry();
  30. entry.title = title;
  31. entry.image = image;
  32. entry.text = text;
  33. entry.kategorie = kategorie;
  34. this.entries.push(entry);
  35. }
  36. }
  37. // Kategorie mit Drop Down Menu wählen
  38. changeKategorie(e) {
  39. console.log(e.value);
  40. this.kategorieName.setValue(e.target.value, {
  41. onlySelf: true
  42. });
  43. }
  44. /*########### Template Driven Form ###########*/
  45. onSubmit() {
  46. this.isSubmitted = true;
  47. if (!this.registrationForm.valid) {
  48. return false;
  49. } else {
  50. alert(JSON.stringify(this.registrationForm.value));
  51. }
  52. }
  53. }