如何在angular2

时间:2017-09-01 10:06:11

标签: angular angular2-template angular2-forms angular2-ngmodel

我是Angular的新手,我正在尝试绑定由模型对象的文本框组成的表单。 但我收到一个错误,如“Unhandled Promise rejection:模板解析错误:无法绑定到'NgModel',因为它不是'input'的已知属性”

我在谷歌投入了大量时间但仍然存在问题

以下是我的代码。

HTML:

    <div>
     <div class="col-sm-6 form-group">
        <label class="control-label">Project Name:</label>
        <input type="text" class="form-control" id="txtProjName" [(NgModel)]="projectFieldsdtl.PROJECT_NAME" >
    </div>

     <div class="col-sm-6 form-group">
        <label class="control-label">Project Manager Name:</label>
        <input type="text" class="form-control" id="txtProjManager" >
    </div>
    <div class="col-sm-6 form-group">
        <label class="control-label">Project Manager Email ID:</label>
        <input type="text" class="form-control" id="txtProjManagerMailID" >
    </div>
    </div>

app.module.ts

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

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

projectFields.ts

        export class projectFields {
                public PROJECT_CODE:string;
                public PROJECT_NAME:string
                public PROJECT_MANAGER: string;
                public PROJECT_MANAGER_EMAIL_ID: string;    
        }

SpotCheckStatusComponent.ts

            import {Component,OnInit ,OnChanges} from '@angular/core';
        import {IMyDpOptions} from 'mydatepicker';
        import {HttpService} from './http.service';
        import { serviceLine } from './models/serviceLine';  
        import { projectFields } from './models/projectFields';  


        @Component({
          selector: 'my-app',
          styleUrls:['/css/home.css'],
          templateUrl:'./SpotCheckStatus.html',
          providers:[HttpService]

        })
        export class SpotCheckStatusComponent  implements OnInit
        {
             name = 'SpotCheckStatus'; 

             public projectFieldsdtl: projectFields[];  

          constructor(
            private httpService: HttpService
          ) {}

          ngOnInit(){

                  this.httpService.getProjectCode(serviceline).subscribe(
                  response=> {      

                    this.projectFieldsdtl=response[0];
                    },
                  error=> {
                      console.log("ERROR: ",error);
                      console.log(error.json()); //gives the object object
                  },
                  () => {
                      console.log("Completed");
                  }

                  );
            }
        }

最后,projectFieldsdtl得到了如下对象:

    [{
    PROJECT_CODE:"9999"
    PROJECT_MANAGER:"shekat"
    PROJECT_MANAGER_EMAIL_ID:"teja.ravi@gmal.com"
    PROJECT_NAME:"ABFL"
    }]

3 个答案:

答案 0 :(得分:4)

你错过了ngmodel中的双引号。

<input type="text" class="form-control" id="txtProjName" [(ngModel)]="projectFieldsdtl.PROJECT_NAME" >

修改

您还在ng-model中使用Array对象。所以它应该像projectFieldsdtl[0].PROJECT_NAME而不是projectFieldsdtl.PROJECT_NAME

由于服务异步调用。在你的div标签上试试这个* ngIf =“projectFieldsdtl!= null”

<div class="col-sm-6 form-group" *ngIf="projectFieldsdtl != null && projectFieldsdtl != undefined">
        <label class="control-label">Project Name:</label>
        <input type="text" class="form-control" id="txtProjName" [(NgModel)]="projectFieldsdtl[0].PROJECT_NAME" >
    </div>

你应该在nginit函数中初始化一个空对象,试试下面的代码

ngOnInit(){
    this.projectFieldsdtl=[{
    PROJECT_CODE:""
    PROJECT_MANAGER:""
    PROJECT_MANAGER_EMAIL_ID:""
    PROJECT_NAME:""
    }];
}

答案 1 :(得分:1)

ngModel区分大小写,因此您应该[(ngModel)]而不是[(ngmodel)]

您的第二个问题是,当您收到undefined模型时,归因于projectFieldsdtl未定义或包含null值。

JS contructor()中考虑将projectFieldsdtl初始化为对象。

// make sure to declare it as well
projectFieldsdtl: any;

constructor()
{
  // initialize its value
  this.projectFieldsdtl = {};
}

希望有所帮助

答案 2 :(得分:0)

由于你从服务获得数组,你可以这样做,然后使用[(ngModel)]

    In Component

    public projectFieldsdtl: projectFields = {};  

    Service Response 

    this.projectFieldsdtl=response[0]

    template 

        <input type="text" class="form-control" id="txtProjName" [(ngModel)]="projectFieldsdtl.PROJECT_NAME" >





Another Way to implement

   <div *ngFor='let item of projectFieldsdtl; let i=index'>
                   <input type="text" class="form-control" id="txtProjName_{{i}}" [(ngModel)]="item .PROJECT_NAME" >
                    </div>

   In component 

   public projectFieldsdtl: projectFields[] = [];  

   Service Response 

   this.projectFieldsdtl=response;