From 6fadb7a3a492ae3b77d68acf60d014879cc901ec Mon Sep 17 00:00:00 2001 From: Santosh Yadav Date: Thu, 14 Nov 2019 13:14:37 +0530 Subject: [PATCH] docs: updated dynamic forms example (#31510) Fixes #31446 PR Close #31510 --- .../examples/dynamic-form/src/app/app.component.ts | 8 +++++--- .../src/app/dynamic-form-question.component.html | 2 +- .../src/app/dynamic-form-question.component.ts | 2 +- .../dynamic-form/src/app/dynamic-form.component.ts | 4 ++-- .../examples/dynamic-form/src/app/question-base.ts | 6 +++++- .../dynamic-form/src/app/question-control.service.ts | 2 +- .../examples/dynamic-form/src/app/question.service.ts | 6 +++--- 7 files changed, 18 insertions(+), 12 deletions(-) diff --git a/aio/content/examples/dynamic-form/src/app/app.component.ts b/aio/content/examples/dynamic-form/src/app/app.component.ts index 51d154cc78..d8e9dd7e29 100644 --- a/aio/content/examples/dynamic-form/src/app/app.component.ts +++ b/aio/content/examples/dynamic-form/src/app/app.component.ts @@ -2,21 +2,23 @@ import { Component } from '@angular/core'; import { QuestionService } from './question.service'; +import { QuestionBase } from './question-base'; +import { Observable } from 'rxjs'; @Component({ selector: 'app-root', template: `

Job Application for Heroes

- +
`, providers: [QuestionService] }) export class AppComponent { - questions: any[]; + questions$: Observable[]>; constructor(service: QuestionService) { - this.questions = service.getQuestions(); + this.questions$ = service.getQuestions(); } } diff --git a/aio/content/examples/dynamic-form/src/app/dynamic-form-question.component.html b/aio/content/examples/dynamic-form/src/app/dynamic-form-question.component.html index 9f1b8cd4a6..e9ae3c8446 100644 --- a/aio/content/examples/dynamic-form/src/app/dynamic-form-question.component.html +++ b/aio/content/examples/dynamic-form/src/app/dynamic-form-question.component.html @@ -11,7 +11,7 @@ - +
{{question.label}} is required
diff --git a/aio/content/examples/dynamic-form/src/app/dynamic-form-question.component.ts b/aio/content/examples/dynamic-form/src/app/dynamic-form-question.component.ts index 9a4c25829f..988e4fb39c 100644 --- a/aio/content/examples/dynamic-form/src/app/dynamic-form-question.component.ts +++ b/aio/content/examples/dynamic-form/src/app/dynamic-form-question.component.ts @@ -9,7 +9,7 @@ import { QuestionBase } from './question-base'; templateUrl: './dynamic-form-question.component.html' }) export class DynamicFormQuestionComponent { - @Input() question: QuestionBase; + @Input() question: QuestionBase; @Input() form: FormGroup; get isValid() { return this.form.controls[this.question.key].valid; } } diff --git a/aio/content/examples/dynamic-form/src/app/dynamic-form.component.ts b/aio/content/examples/dynamic-form/src/app/dynamic-form.component.ts index 4167b4617f..cca09063e3 100644 --- a/aio/content/examples/dynamic-form/src/app/dynamic-form.component.ts +++ b/aio/content/examples/dynamic-form/src/app/dynamic-form.component.ts @@ -12,7 +12,7 @@ import { QuestionControlService } from './question-control.service'; }) export class DynamicFormComponent implements OnInit { - @Input() questions: QuestionBase[] = []; + @Input() questions: QuestionBase[] = []; form: FormGroup; payLoad = ''; @@ -23,6 +23,6 @@ export class DynamicFormComponent implements OnInit { } onSubmit() { - this.payLoad = JSON.stringify(this.form.value); + this.payLoad = JSON.stringify(this.form.getRawValue()); } } diff --git a/aio/content/examples/dynamic-form/src/app/question-base.ts b/aio/content/examples/dynamic-form/src/app/question-base.ts index 8249a38bee..8edc0dcfdc 100644 --- a/aio/content/examples/dynamic-form/src/app/question-base.ts +++ b/aio/content/examples/dynamic-form/src/app/question-base.ts @@ -6,6 +6,8 @@ export class QuestionBase { required: boolean; order: number; controlType: string; + type: string; + options: {key: string, value: string}[]; constructor(options: { value?: T, @@ -13,7 +15,8 @@ export class QuestionBase { label?: string, required?: boolean, order?: number, - controlType?: string + controlType?: string, + type?: string } = {}) { this.value = options.value; this.key = options.key || ''; @@ -21,5 +24,6 @@ export class QuestionBase { this.required = !!options.required; this.order = options.order === undefined ? 1 : options.order; this.controlType = options.controlType || ''; + this.type = options.type || ''; } } diff --git a/aio/content/examples/dynamic-form/src/app/question-control.service.ts b/aio/content/examples/dynamic-form/src/app/question-control.service.ts index 1378ba8490..1ca0727055 100644 --- a/aio/content/examples/dynamic-form/src/app/question-control.service.ts +++ b/aio/content/examples/dynamic-form/src/app/question-control.service.ts @@ -8,7 +8,7 @@ import { QuestionBase } from './question-base'; export class QuestionControlService { constructor() { } - toFormGroup(questions: QuestionBase[] ) { + toFormGroup(questions: QuestionBase[] ) { let group: any = {}; questions.forEach(question => { diff --git a/aio/content/examples/dynamic-form/src/app/question.service.ts b/aio/content/examples/dynamic-form/src/app/question.service.ts index a1d9499c68..7f80dd78c1 100644 --- a/aio/content/examples/dynamic-form/src/app/question.service.ts +++ b/aio/content/examples/dynamic-form/src/app/question.service.ts @@ -4,15 +4,15 @@ import { Injectable } from '@angular/core'; import { DropdownQuestion } from './question-dropdown'; import { QuestionBase } from './question-base'; import { TextboxQuestion } from './question-textbox'; +import { of } from 'rxjs'; @Injectable() export class QuestionService { // TODO: get from a remote source of question metadata - // TODO: make asynchronous getQuestions() { - let questions: QuestionBase[] = [ + let questions: QuestionBase[] = [ new DropdownQuestion({ key: 'brave', @@ -42,6 +42,6 @@ export class QuestionService { }) ]; - return questions.sort((a, b) => a.order - b.order); + return of(questions.sort((a, b) => a.order - b.order)); } }