Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | 1x 4x 4x 8x 4x 1x 4x 1x 14x 14x 14x 14x 14x 14x 14x 14x 14x 1x 15x 1x 18x 18x 18x 28x 18x 1x 44x 133x 133x 133x 133x 1x 1x 1x 1x 1x 3x 3x 2x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x | import {Component, EventEmitter, Input, NgZone, OnInit, Output} from '@angular/core'; import {FormBuilder, FormGroup, Validators} from '@angular/forms'; I import {Contact} from '../../interfaces';E /* * Contact Form */E @Component({ selector: 'app-contact-form', templateUrl: './contact-form.component.html' }) export class ContactFormComponent implements OnInit { /* * Back Url */ @Input() backUrl: string[] = []; /* * Model reference */ @Input() model: Contact = { firstName: '', lastName: '', company: '' }; /* * Submit output */ @Output() save: EventEmitter<Contact> = new EventEmitter<Contact>(); /* * Form holder */ form: FormGroup; /* * Local form errors */ formErrors: any = { firstName: '', lastName: '', company: '' }; /* * Common form errors */ private readonly errors: any = { required: 'Field is required' }; /* * Constructor * * @param zone NgZone * @param formBuilder FormBuilder */ constructor( private zone: NgZone, private formBuilder: FormBuilder ) { this.createForm(); this.form.setValue(this.model); } /* * ngOnInit */ ngOnInit() { this.form.setValue({ firstName: this.model.firstName, lastName: this.model.lastName, company: this.model.company }); } /* * Creates form */ createForm() { this.form = this.formBuilder.group({ firstName: ['', [Validators.required]], lastName: ['', [Validators.required]], company: [''] }); this.form.valueChanges .subscribe(() => this.onValueChanged()); this.onValueChanged(); } /* * Reacts on every value change */ onValueChanged() { for (const field of Object.keys(this.formErrors)) { this.formErrors[field] = ''; const control = this.form.get(field); if (control && control.dirty && !control.valid) { for (const key of Object.keys(control.errors)) { this.formErrors[field] = this.errors[key]; break; } } } } /* * Main submit method */ onSubmit() { if (this.form.invalid) { this.zone.run(() => { for (const key of Object.keys(this.form.controls)) { this.form.controls[key].markAsDirty(); } this.onValueChanged(); }); return; } this.save.emit(this.form.value); this.form.reset(); } } |