当前位置: 首页 > 知识库问答 >
问题:

角绑定错误-不能绑定到'ng模型',因为它不是'input'的已知属性,即使属性存在

蔺劲
2023-03-14
  export class ProjectsComponent implements OnInit {

  projects : Project[];
  newProject : Project = new Project();
  constructor(private projectSevice : ProjectsService) {

   }
 <div class="form-group row">
            <label for="txtNewProjectID" class="col-sm-4 col-form-label">Project ID</label>
            <div class="col-sm-8">
              <input type="text" id="txtNewProjectID" style="width:100px" class="form-control" placeholder="Project ID"
                name="ProjectID" [(ngModel)]="newProject.projectID">
            </div>
          </div>

          <div class="form-group row">
            <label for="txtNewProjectName" class="col-sm-4 col-form-label">Project Name</label>
            <div class="col-sm-8">
              <input type="text" id="txtNewProjectName" class="form-control" placeholder="Project Name"
                name="ProjectName" [(ngModel)]="newProject.projectName">
            </div>
          </div>

下面是项目类

export class Project {

    projectID: number;
    projectName: string;
    dateOfStart: string;
    teamSize: number;

    constructor(){
        this.projectID = 0;
        this.projectName = null;
        this.dateOfStart = null;
        this.teamSize = 0;
    }
}

我弄不清我做错了什么

共有1个答案

孟开宇
2023-03-14

为了使用ngModel指令和与Angular的通用数据绑定,您需要首先在模块中导入Angular的FormsModule。

要做到这一点,请知道您的组件包含什么模块。然后在导入列表中添加FormsModule。

例如,您可以将其导入app.module.ts:

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    AppRoutingModule,
    NgbModule,
    FormsModule // Insert here
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
import { FormsModule } from '@angular/forms';
 类似资料: