元素隐式具有“ any”类型,因为ANGULAR中的type表达式

诺曼·斯塔洛奇

更新列表时我遇到错误,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}`)
  }
}
Basheer Kharoti

您不需要重新创建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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

元素隐式具有“ any”类型,因为索引表达式不是“ number”类型[7015]

元素隐式地具有“ any”类型,因为类型“ any”的表达式不能用于索引类型

元素隐式具有 any 类型,因为时间表达式 any 不能用于索引类型

元素隐式具有“any”类型,因为“string”类型的表达式不能用于索引“Breakpoints”类型

元素隐式地具有“ any”类型,因为类型“ string”的表达式不能用于索引类型

元素隐式具有“any”类型,因为“string”类型的表达式不能用于索引“Palette”类型

元素隐式具有 'any' 类型,因为类型 'string' 的表达式不能用于索引类型 '{ AT: number; 是:数字,...}`

Typescript错误:元素隐式具有“ any”类型,因为类型“ string”的表达式不能用于索引类型

TypeScript - 元素隐式具有“any”类型,因为“storyFile”类型的表达式不能用于索引类型“{}”

元素隐式具有“any”类型,因为“string”类型的表达式不能用于索引“PointDto”类型

该元素隐式具有类型“any”,因为类型“string”的表达式不能用于索引该类型

TypeScript TS7015:元素隐式地具有“ any”类型,因为索引表达式不是“ number”类型

Build:元素隐式地具有“ any”类型,因为索引表达式不是“ number”类型

元素隐式地具有“ any”类型,因为索引表达式不是“ number”类型。-索引签名错误

元素隐式具有“任何”类型,因为“字符串”类型的表达式

map()中的Typescript错误:元素隐式具有“ any”类型,因为类型“ string”的表达式不能用于索引类型

隐式具有“任何”类型,因为 typescript 中的类型表达式

元素隐式具有“any”类型,因为“Tag”类型的表达式不能用于索引类型“{}”。类型“{}”上不存在属性“html”

元素隐式具有“任意”类型,因为类型“字符串”的表达式不能用于索引类型A

TypeScript-元素隐式地具有“ any”类型,因为类型“ string”的表达式不能用于索引类型

元素隐式具有 'any' 类型,因为类型 'string' 的表达式不能用于索引类型 {"1" : { "key": string}; "2" : { "key": 字符串};}

元素隐式具有 'any' 类型,因为类型 'string' 的表达式不能用于索引类型 '{property: string, property: string }'

React Typescript:元素隐式具有“任何”类型,因为索引表达式不是“数字”类型

元素隐式具有“任意”类型,因为类型“字符串”的表达式不能用于索引

全局窗口对象:元素隐式具有“任何”类型,因为索引表达式不是“数字”类型。ts

元素隐式地具有“ any”类型,因为在打字稿中将字符串转换为enum时,索引表达式不是enum的“ number”类型

Object.keys迭代导致Typescript错误“元素隐式具有'any'类型,因为索引表达式不是'number'类型”

Angular - 元素隐式具有“any”类型,因为“AbstractControl”类型没有索引签名

Angular - 元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型