MMR
MMR

Reputation: 3009

" setValue does not exists on type AbstractControl" for emptying form in angular2

My template,

  <form [formGroup]="form" (ngSubmit)="onSubmit(form.value)" novalidate="novalidate">
        <textarea [ngClass]="{ 'error': comment }"  
                  [formControl]="form.controls['comment']"  
                  placeholder="Comment...."></textarea>
    <div class="form-group">
       <button type="submit" id="template-contactform-submit" 
               name="template-contactform-submit"  
               value="submit">Enter</button>
    </div> 

my TS:

for(var prop in form) {
   if(this.form.controls.hasOwnProperty(prop)) {
      this.form.controls[prop].setValue(" ");
   }
}

I am getting the error as " setValue does not exists on type AbstractControl".

Upvotes: 0

Views: 4072

Answers (2)

Radim K&#246;hler
Radim K&#246;hler

Reputation: 123851

I would suggest to double check the error you are getting.

Firstly, just a sanity check - the error message shown in question is:

setValue does not exists on type AbstractControl

wile TS compiler would say

Property 'setValue' does not exist on type 'AbstractControl'

so, the inconsistency leads to assurance if the wrong method call is really setValue, and not something else.

Secondly - the setValue DOES EXIST on AbstractControl. Check the AbstractControl source

export abstract class AbstractControl {
    ...
    /**
     * Sets the value of the control. Abstract method (implemented in sub-classes).
     */
    abstract setValue(value: any, options?: Object): void;

Upvotes: 0

Ankit Singh
Ankit Singh

Reputation: 24945

Try

(<FormControl>this.form.controls[prop]).setValue(" ");

also import FormControl to make it work as

import {FormControl} from '@angular/forms';

Upvotes: 1

Related Questions