首页 文章

如何将对象属性绑定到angular2中的文本框

提问于
浏览
1

我是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 回答

  • 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

    ngModel 区分大小写,所以你应该放 [(ngModel)] 而不是 [(ngmodel)] .

    你的第二个问题是当你收到 undefined 模型时,它是由于 projectFieldsdtl 未定义或包含 null 值 .

    在你的 JS contructor() 中考虑将 projectFieldsdtl 初始化为对象 .

    // make sure to declare it as well
    projectFieldsdtl: any;
    
    constructor()
    {
      // initialize its value
      this.projectFieldsdtl = {};
    }
    

    希望有所帮助

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

相关问题