【发布时间】:2016-09-23 17:05:58
【问题描述】:
这是我的代码;
服务:
import { Injectable } from "@angular/core";
import { Http, Headers, RequestOptions } from "@angular/http";
import { Observable } from "rxjs/Observable";
import 'rxjs/Rx';
import {Heatmap} from "./heatmap"
@Injectable()
export class HeatmapService {
constructor (private _http: Http) {}
signup(heatmap: Heatmap) {
const body = JSON.stringify(heatmap);
const headers = new Headers({'Content-Type': 'application/json'});
const options = new RequestOptions({ headers: headers });
return this._http.post('http://localhost:8080/create', body, options)
.map(response => response.json())
.catch(error => Observable.throw(error.json()));
}
}
组件:
import { Component, OnInit } from "@angular/core";
import { FormBuilder, FormGroup, FormControl, Validators } from "@angular/forms";
import { Heatmap } from "./heatmap";
import { HeatmapService } from "./heatmap.service";
@Component({
templateUrl: 'js/app/heatmap/heatmap.component.html'
})
export class heatmapComponent implements OnInit {
myForm: FormGroup;
constructor(private formBuilder: FormBuilder, public heatService: HeatmapService) {}
onSubmit() {
const heatmap = new Heatmap(this.myForm.value.name, this.myForm.value.data, this.myForm.value.country);
console.log(heatmap)
this.heatService.sendData(heatmap)
.subscribe(
data => console.log(data),
error => console.error(error)
)
}
ngOnInit() {
this.myForm = this.formBuilder.group({
name: ['', Validators.required],
data: ['', Validators.required],
country: ['', Validators.required]
});
}
}
HTML:
<form [formGroup]="myForm" (ngSubmit)="onSubmit()">
<div class="form-group">
<label for="name">name</label>
<input type="text" id="name" class="form-control" formControlName="name">
</div>
<div class="form-group">
<label for="data">data</label>
<input type="text" id="data" class="form-control" formControlName="data">
</div>
<div class="form-group">
<label for="country">country</label>
<input type="text" id="country" class="form-control" formControlName="country">
</div>
<button type="submit" class="btn btn-primary" [disabled]="!myForm.valid">Sign Up</button>
</form>
热图:
export class Heatmap {
constructor(public name: string, public data: string, public country: string){}
}
问题:
每次我提交表单时,我的服务器都会自动重新启动,而不会记录任何数据,并且没有给我时间查看任何正在处理的错误。
我得到的唯一错误是 gulp 的输出 -
错误 TS2339:“HeatmapService”类型上不存在属性“sendData”。
希望有人能帮忙!
【问题讨论】: