Angular 4表单[(ngModel)]错误

时间:2017-11-09 12:32:49

标签: angular mean-stack angular-ngmodel angular4-forms

我正在使用angular 4 meanstack。当我输入[(ngModel)]时,我的表单显示错误。然后我尝试"ngmodel"然后表单工作正常。但是后端未定义数据。

错误如下。 当我使用[(ngModel)]

<form (submit)="savereportdata()" novalidate>

<fieldset>

    <div class="form-group">
        <label for="middleItemId">Item ID</label>
        <input type="text" class="form-control" placeholder="Enter Item ID" [(ngModel)]="itemid" name="itemid">
    </div>
    <div class="form-group">
        <label for="middleItemId">type</label>
        <input type="text" class="form-control" placeholder="Enter Item ID" [(ngModel)]="typeofdefect" name="typeofdefect">
    </div>

错误:

 VM3014 ReportComponent.ngfactory.js:7 ERROR TypeError: Cannot read property 'reportitemdata' of undefined
        at ReportComponent.webpackJsonp.../../../../../src/app/pages/requestreport/report/report.component.ts.ReportComponent.savereportdata (VM2980 main.bundle.js:983)
        at Object.eval [as handleEvent] (VM3014 ReportComponent.ngfactory.js:12)
        at handleEvent (VM2979 vendor.bundle.js:58441)
        at callWithDebugContext (VM2979 vendor.bundle.js:59905)
        at Object.debugHandleEvent [as handleEvent] (VM2979 vendor.bundle.js:59493)
        at dispatchEvent (VM2979 vendor.bundle.js:55034)
        at VM2979 vendor.bundle.js:55645
        at HTMLFormElement.<anonymous> (VM2979 vendor.bundle.js:64132)
        at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask (VM2976 polyfills.bundle.js:2970)
        at Object.onInvokeTask (VM2979 vendor.bundle.js:50300)

当我使用ngmodel

<div class="form-group">
                <label for="middleItemId">Item ID</label>
                <input type="text" class="form-control" placeholder="Enter Item ID" [(ngModel)]="itemid" name="itemid">
            </div>

错误:

 Uncaught Error: Template parse errors: Can't bind to 'ngModel' since it isn't a known property of 'input'. ("Id">Item ID</label>
                <input type="text" class="form-control" placeholder="Enter Item ID" [ERROR ->][(ngModel)]="itemid" name="itemid">
            </div>
            <div class="form-group"> "): ng:///AppModule/ReportComponent.html@6:80
        at syntaxError (compiler.es5.js:1694)
        at TemplateParser.webpackJsonp.../../../compiler/@angular/compiler.es5.js.TemplateParser.parse (compiler.es5.js:12932)
        at JitCompiler.webpackJsonp.../../../compiler/@angular/compiler.es5.js.JitCompiler._compileTemplate (compiler.es5.js:27126)
        at compiler.es5.js:27045
        at Set.forEach (<anonymous>)
        at JitCompiler.webpackJsonp.../../../compiler/@angular/compiler.es5.js.JitCompiler._compileComponents (compiler.es5.js:27045)
        at compiler.es5.js:26932
        at Object.then (compiler.es5.js:1683)
        at JitCompiler.webpackJsonp.../../../compiler/@angular/compiler.es5.js.JitCompiler._compileModuleAndComponents (compiler.es5.js:26931)
        at JitCompiler.webpackJsonp.../../../compiler/@angular/compiler.es5.js.JitCompiler.compileModuleAsync (compiler.es5.js:26860)

我的component.ts文件

import { NgModule,Component,Pipe,OnInit } from '@angular/core';
import { ReportserviceService } from '../../../service/reportservice.service'


@Component({
  selector: 'app-report',
  templateUrl: './report.component.html',
  styleUrls: ['./report.component.css']
})

export class ReportComponent implements OnInit {

  itemid:Number;
  typeofdefect:String;
  reportdescription:String;
  constructor() { }

public reportservice:ReportserviceService;


  ngOnInit() {
  }


  savereportdata(){   

  const reportitem = {
    itemid:this.itemid,
    typeofdefect:this.typeofdefect,
    reportdescription:this.reportdescription
  };


   this.reportservice.reportitemdata(reportitem).subscribe(res=>{
    console.log(res);
  }); 

  }

}

我的app.module.ts文件

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

import { ReportserviceService } from './service/reportservice.service';

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


const applicationRoutes:Routes=[
{path:'report',component:ReportComponent},
{path:'requestexisting',component:ExistingitemComponent},
{path:'requestnew',component:NewitemComponent},
{path:'allocateproperties',component:AllocatepropertiesComponent}
];

@NgModule({
  declarations: [
    AppComponent,
    LoginComponent,
    //more codes

  ],
  imports: [
    BrowserModule,
    FormsModule,
    NgModule,
    RouterModule.forRoot(applicationRoutes)
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { } 

我已经找了整整一个月的解决方案。但仍然无法解决它。

my @angular/cli:1.4.2
node: 6.10.3
os: win32 x64

1 个答案:

答案 0 :(得分:0)

将服务放在构造函数

constructor(public reportservice:ReportserviceService) { }


//Add in app.module.ts
import { HttpModule } from '@angular/http';
imports :[HttpModule]

//Add in service 
import { Http } from '@angular/http';
inject http in construtor like this
constructor(private http : Http);