1

I'm trying to implement Cancel functionality which should revert back to the previous values in the form, which were present during the last time the form was submitted. I'm trying to create a copy of the data object that's being passed to the form and in the Submit function i'm replacing the new values to the copy and in Cancel function, i'm replacing the copy values to the original values. But i'm not getting the previous values when i call the cancel function.

Working code with errors: http://plnkr.co/edit/SL949g1hQQrnRUr1XXqt?p=preview

I implemented the cancel functionality based on the template driven code of Angualr 2 forms: http://plnkr.co/edit/LCAPYTZElQDjrSgh3xnT?p=preview

Typescript class code:

import { Component, Input, OnInit }  from '@angular/core';
import { FormGroup, REACTIVE_FORM_DIRECTIVES } from '@angular/forms';
import { QuestionBase }                 from './question-base';
import { QuestionControlService }       from './question-control.service';

@Component({
  selector: 'dynamic-form',
  templateUrl: 'app/dynamic-form.component.html',
  directives: [REACTIVE_FORM_DIRECTIVES],
  providers:  [QuestionControlService]
})
export class DynamicFormComponent implements OnInit {

  @Input() questions: QuestionBase<any>[] = [];
  form: FormGroup;
  payLoad:object;
  questiont: QuestionBase<any>;
  constructor(private qcs: QuestionControlService) {  }
  ngOnInit() {
    this.form = this.qcs.toFormGroup(this.questions);
    console.log("Form Init",this.questions);
    this.questiont=this.questions;
  }
  onSubmit() {
    this.payLoad = JSON.stringify(this.form.value);
    this.payLoad2=this.payLoad;
    this.questiont=this.questions;
    console.log("Original Data",this.questions);
    console.log("Duplicate Data",this.questiont);
  }
  cancel(){
    this.questions=this.questiont;
    console.log("Original Data",this.questions);
    console.log("Duplicate Data",this.questiont);
    console.log("Canceled");
  }

}

HTML code:

<div>
  <form [formGroup]="form">

    <div *ngFor="let question of questions" class="form-row">
      <label [attr.for]="question.key">{{question.label}}</label>

  <div [ngSwitch]="question.controlType">

    <input *ngSwitchCase="'textbox'" [formControlName]="question.key"
            [id]="question.key" [type]="question.type" [(ngModel)]="question.value">

    <select [id]="question.key" [(ngModel)]="question.value" *ngSwitchCase="'dropdown'" [formControlName]="question.key" >
      <option *ngFor="let opt of question.options" [ngValue]="opt.key" >{{opt.value}}</option>
    </select>

  </div> 
  <div class="errorMessage" *ngIf="!form.controls[question.key].valid">{{question.label}} is required</div>
    </div>

    <div class="form-row">
      <button type="submit" [disabled]="!form.valid" (click)="onSubmit()">Save</button>
      <button type="button" class="btn btn-default" (click)="cancel()">Cancel</button>

    </div>
  </form>

  <div *ngIf="payLoad" class="form-row">
    <strong>Saved the following values</strong><br>{{payLoad}}
  </div>
</div>

Anyone got this problem or tried to implement this.

1 Answer 1

7

Form reset will be implemented in RC5.

Your approach does not work with objects:

this.questiont=this.questions; //you are sharing refrence to the same object

You should clone object instead (there are many ways to do that, i'm using JSON):

this.questiont = JSON.parse(JSON.stringify(this.questions));
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the answer, it's working. I'm trying to implement clear functionality, which for the time being clears all the values in the form shown, so that user can enter completely new data. Is that possible do with the above code? Can you please shed some light regarding that clear functionality. Thanks
Hey Do you have any idea, how to implement the clear functionality? Help me over here. Thanks.
currently you need to create copy of your model to implement clear, the same way as in my answer
Working fine! Is there any builtin way that angular handles this functionality?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.