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.

blog-create.component.ts 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { Component, OnInit } from '@angular/core';
  2. import {BlogEntry} from "../blog-entry/blog-entry";
  3. import {FormBuilder, Validators} from "@angular/forms";
  4. import {initialEntries} from "../blog-entry/initialEntries";
  5. @Component({
  6. selector: 'app-blog-create',
  7. templateUrl: './blog-create.component.html',
  8. styleUrls: ['./blog-create.component.css']
  9. })
  10. export class BlogCreateComponent implements OnInit {
  11. title = 'Einkaufszettel';
  12. entries: BlogEntry[] = [];
  13. isSubmitted = false;
  14. Kategorien: any = ['Lebensmittel', 'Spielwaren', 'Technik', 'Sonstiges'];
  15. constructor(public fb: FormBuilder) {
  16. this.entries = [];
  17. this.entries = initialEntries;
  18. }
  19. // Getter method to access formcontrols
  20. get kategorieName() {
  21. return this.registrationForm.get('kategorieName');
  22. }
  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.einkaufsort = 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. deleteAllEntries() {
  42. console.log(this.entries.length);
  43. const index: number = this.entries.length;
  44. if (index !== -1) {
  45. this.entries = this.entries.splice(index, 1);
  46. }
  47. }
  48. deleteLastEntry() {
  49. console.log(this.entries.length);
  50. const index: number = this.entries.length;
  51. if (index !== -1) {
  52. this.entries.splice(index-1, 1);
  53. }
  54. }
  55. // Kategorie mit Drop Down Menu wählen
  56. changeKategorie(e) {
  57. console.log(e.value);
  58. this.kategorieName.setValue(e.target.value, {
  59. onlySelf: true
  60. });
  61. }
  62. /*########### Template Driven Form ###########*/
  63. onSubmit() {
  64. this.isSubmitted = true;
  65. if (!this.registrationForm.valid) {
  66. return false;
  67. } else {
  68. alert(JSON.stringify(this.registrationForm.value));
  69. }
  70. }
  71. ngOnInit(): void {
  72. }
  73. }