更新列表时我遇到错误,ReactForm这是我附加的代码,请为此提供帮助...在此先感谢我的代码::
import { CommonService } from './../common.service';
import { Component, OnInit } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';
import { ActivatedRoute } from '@angular/router';
@Component({
selector: 'app-update-resturant',
templateUrl: './update-resturant.component.html',
styleUrls: ['./update-resturant.component.css']
})
export class UpdateResturantComponent implements OnInit {
alert : Boolean = false;
updateResto = new FormGroup({
Name: new FormControl(''),
Email: new FormControl('')
})
constructor(private resto:CommonService, private router: ActivatedRoute) { }
ngOnInit(): void {
this.resto.getCurrentData(this.router.snapshot.params.id).subscribe((result)=> {
console.log(result)
this.updateResto = new FormGroup({
Name: new FormControl(result['Name']),
Email: new FormControl(result['Email'])
})
}
}
错误::
元素隐式地具有“ any”类型,因为类型“” Email””的表达式不能用于索引类型” Object”。类型“对象”上不存在属性“电子邮件”。
服务代码:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class CommonService {
URL= "http://localhost:3000/list"
constructor(private _http:HttpClient) { }
getRestoList(){
return this._http.get(this.URL)
}
addResto(data: any){
return this._http.post(this.URL, data);
}
// deleteCurrentData(id: any){
// return this._http.delete(`${this.URL}/${id}`)
// }
getCurrentData(id: number){
return this._http.get(`${this.URL}/${id}`)
}
}
您不需要重新创建FormGroup,而只需更新FormControl值。结帐反应式表单指南
ngOnInit(): void {
this.resto.getCurrentData(this.router.snapshot.params.id).subscribe((result)=> {
this.updateResto.controls['Name'].setValue(result['Name']);
this.updateResto.controls['Email'].setValue(result['Email']);
// OR you can update them all at once
this.updateResto.setValue({
Name: result['Name'],
Email: result['Email']
})
}
请参阅以下示例供您参考。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句