forms - react - Angular 2:由於它不是'input'的已知屬性,因此無法綁定到'ngModel'




react form angular (10)

升級到RC5後,我遇到了類似的錯誤; 即Angular 2:由於它不是'input'的已知屬性,因此無法綁定到'ngModel'。

Plunker上的代碼向您展示瞭如何使用Angular2 RC4,但 https://angular.io/docs/ts/latest/cookbook/dynamic-form.html 的示例代碼使用的是RC5的NGModule。 NGModules是從RC4到RC5的重大變化。

本頁說明了從RC4到RC5的遷移: https://angular.io/docs/ts/latest/cookbook/rc4-to-rc5.html : https://angular.io/docs/ts/latest/cookbook/rc4-to-rc5.html

我希望這能解決您遇到的錯誤,並幫助您朝正確的方向前進。

簡而言之,我必須在app.module.ts中創建一個NGModule:

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

import { AppComponent }  from './app.component';

@NgModule({
    imports:      [ BrowserModule, FormsModule ],
    declarations: [ AppComponent ],
    bootstrap:    [ AppComponent ]
})
export class AppModule { }

然後,我更改了main.ts以使用該模塊:

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';

platformBrowserDynamic().bootstrapModule(AppModule);

當然,我還需要更新package.json中的依賴項。 這是我對package.json的依賴。 誠然,我從其他來源(也許是ng docs示例)將它們整合在一起,所以您的里程可能會有所不同:

...
"dependencies": {
    "@angular/common": "2.0.0-rc.5",
    "@angular/compiler": "2.0.0-rc.5",
    "@angular/core": "2.0.0-rc.5",
    "@angular/forms": "0.3.0",
    "@angular/http": "2.0.0-rc.5",
    "@angular/platform-browser": "2.0.0-rc.5",
    "@angular/platform-browser-dynamic": "2.0.0-rc.5",
    "@angular/router": "3.0.0-rc.1",
    "@angular/router-deprecated": "2.0.0-rc.2",
    "@angular/upgrade": "2.0.0-rc.5",
    "systemjs": "0.19.27",
    "core-js": "^2.4.0",
    "reflect-metadata": "^0.1.3",
    "rxjs": "5.0.0-beta.6",
    "zone.js": "^0.6.12",
    "angular2-in-memory-web-api": "0.0.15",
    "bootstrap": "^3.3.6"
  },
...

我希望這會有所幫助。 :-)

我正在嘗試在Angular 2中實現動態表單。我向動態表單添加了其他功能,例如刪除和取消。 我已遵循此文檔: https://angular.io/docs/ts/latest/cookbook/dynamic-form.html : https://angular.io/docs/ts/latest/cookbook/dynamic-form.html

我對代碼做了一些更改。 我在這裡出錯。

我如何使此錯誤消失?

您可以在此處找到完整的代碼: http://plnkr.co/edit/SL949g1hQQrnRUr1XXqt?p=preview ://plnkr.co/edit/SL949g1hQQrnRUr1XXqt?p=preview,它可以在插件中使用,但不能在我的本地系統中使用。

HTML代碼:

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

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

  </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>
      <button type="button" class="btn btn-default" (click)="clear()">Clear</button>

    </div>
  </form>

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

組件代碼:

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

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

  @Input() questions: QuestionBase<any>[] = [];
  form: FormGroup;
  payLoad:any;
  payLoad2:any;
  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.payLoad2=this.payLoad;
    this.questiont = JSON.parse(JSON.stringify(this.questions));
    console.log("Submitted data",this.questions);
  }
  cancel(){
    console.log("Canceled");
    this.questions = JSON.parse(JSON.stringify(this.questiont));
  }
  clear(){
    this.questions = JSON.parse(JSON.stringify(this.questiond));
    this.questiont = JSON.parse(JSON.stringify(this.questiond));
    console.log("Cleared");
    this.cdr.detectChanges();
  }
}


您只需添加 FormsModule 並將 FormsModule 導入到 app.module.ts 文件中。

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

imports: [
    BrowserModule, FormsModule 
],

只需將上述兩行添加到您的 app.module.ts 。 一切正常。


您需要在@NgModule裝飾器中導入FormsModule,@ NgModule在moduleName.module.ts文件中。

import { FormsModule } from '@angular/forms';
@NgModule({
   imports: [
      BrowserModule,
      FormsModule
   ],
   declarations: [ AppComponent ],
   bootstrap: [ AppComponent ]
 })

找出快速解決方案,像這樣更新您的@NgModule代碼:

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

@NgModule({
imports: [ BrowserModule, FormsModule ],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})

export class AppModule { }

來源: 無法綁定到“ ngModel”,因為它不是“ input”的已知屬性


為了在使用AppModules(NgModule)時使ngModel工作,您必須在AppModule中導入FormsModule。

像這樣:

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

import { AppComponent }   from './app.component';


@NgModule({
    declarations: [AppComponent],
    imports: [BrowserModule, FormsModule],
    bootstrap: [AppComponent]
})
export class AppModule {}

由於某些原因,在Angular 6中,僅導入FormsModule不能解決我的問題。 最終解決我問題的方法是添加

import { CommonModule } from '@angular/common';

@NgModule({
  imports: [CommonModule],
})

export class MyClass{
}

遵循步驟

1.打開 app.module.ts 文件。

2. import { FormsModule } from '@angular/forms'; 添加 import { FormsModule } from '@angular/forms';

3.將 FormsModule 作為 imports: [ BrowserModule, FormsModule ], 添加到 imports imports: [ BrowserModule, FormsModule ],

最終結果將如下所示

.....
import { FormsModule } from '@angular/forms';
.....
@NgModule({
.....
  imports: [   
     BrowserModule, FormsModule 
  ],
.....
})

假設您的舊app.module.ts可能類似於以下內容:

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

@NgModule({
    imports: [ BrowserModule ],
    declarations: [ AppComponent ],
    bootstrap: [ AppComponent ]
})

export class AppModule { }

現在在您的app.module.ts中導入FormsModule

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

@NgModule({
    imports: [ BrowserModule, FormsModule ],
    declarations: [ AppComponent ],
    bootstrap: [ AppComponent ]
})

export class AppModule { }

http://jsconfig.com/solution-cant-bind-ngmodel-since-isnt-known-property-input/


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

import {FormsModule,ReactiveFormsModule} from '@angular/forms';

您還應該添加丟失的內容。







angular2-forms