Varun Varun - 1 year ago 202
AngularJS Question

Angular 2: Can't bind to 'ngModel' since it isn't a known property of 'input'

I'm trying to implement Dynamic Forms in Angular 2. I've added additional functionalities like Delete and Cancel to the dynamic forms.
I've followed this documentation:

enter image description here

I've made some changes to the code. I'm getting error here.

How do i make this error go?

You can find the full code here: , which is working in plunker but not in my local system.

Html code:

<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>

<input *ngSwitchCase="'checkbox'" [(ngModel)]="question.value"
[id]="question.key" [type]="question.type" (change)="question.value = ck.checked" #ck [ngModelOptions]="{standalone: true}">

<div class="errorMessage" *ngIf="!form.controls[question.key].valid">{{question.label}} is required</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>
<button type="button" class="btn btn-default" (click)="clear()">Clear</button>


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

Component 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';
import { ControlGroup } from '@angular/common';
import {ChangeDetectorRef} from '@angular/core';
import { FormsModule } from '@angular/forms';

selector: 'dynamic-form',
templateUrl: 'app/dynamicform/form.component.html',
providers: [QuestionControlService]
export class DynamicFormComponent implements OnInit {

@Input() questions: QuestionBase<any>[] = [];
form: FormGroup;
questiont: QuestionBase<any>;
questiond: QuestionBase<any>;
constructor(private qcs: QuestionControlService, private cdr: ChangeDetectorRef) { }
ngOnInit() {
this.form = this.qcs.toFormGroup(this.questions);
console.log("Form Init",this.questions);
this.questiont = JSON.parse(JSON.stringify(this.questions));
this.questiond = JSON.parse(JSON.stringify(this.questions));
onSubmit() {
this.payLoad = JSON.stringify(this.form.value);
this.questiont = JSON.parse(JSON.stringify(this.questions));
console.log("Submitted data",this.questions);
this.questions = JSON.parse(JSON.stringify(this.questiont));
this.questions = JSON.parse(JSON.stringify(this.questiond));
this.questiont = JSON.parse(JSON.stringify(this.questiond));

Answer Source

Found this solution while Googling, update your @NgModule code like this :

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';

imports: [ BrowserModule, FormsModule ],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]

export class AppModule { }

Source: Can’t bind to ‘ngModel’ since it isn’t a known property of ‘input’

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download