Angular 以动态方式将属性过帐到API
我正在尝试创建一个服务,可以将文档中的单个字段放回服务器 我有这个:Angular 以动态方式将属性过帐到API,angular,Angular,我正在尝试创建一个服务,可以将文档中的单个字段放回服务器 我有这个: updateProperty(field: string, value: string): Observable<Property> { return this.http.put<Property>(`${this.apiUrl}/property`, { field, value }); } 它将创建以下标题: field : "Name" value : "John" 如何更改此项,使标
updateProperty(field: string, value: string): Observable<Property> {
return this.http.put<Property>(`${this.apiUrl}/property`, { field, value });
}
它将创建以下标题:
field : "Name"
value : "John"
如何更改此项,使标题变为:
"Name" : "John"
field: "John"
更新
当我尝试{field:value}时,标题变成:
"Name" : "John"
field: "John"
任何对象都可以解析为“数组”语法样式:
const field = "name";
const value = "John";
const data = {};
data[field] = value;
console.log(data);
所以你可以这样做:
updateProperty(field: string, value: string): Observable<Property> {
let header = {};
header[field] = value;
return this.http.put<Property>(`${this.apiUrl}/property`, header);
}
updateProperty(字段:字符串,值:字符串):可观察{
让标头={};
标题[字段]=值;
返回this.http.put(`${this.apirl}/property`,header);
}
任何对象都可以解析为“数组”语法样式:
const field = "name";
const value = "John";
const data = {};
data[field] = value;
console.log(data);
所以你可以这样做:
updateProperty(field: string, value: string): Observable<Property> {
let header = {};
header[field] = value;
return this.http.put<Property>(`${this.apiUrl}/property`, header);
}
updateProperty(字段:字符串,值:字符串):可观察{
让标头={};
标题[字段]=值;
返回this.http.put(`${this.apirl}/property`,header);
}
这与对象分解的工作方式有关{field,value}
实际上是指{field:field,value:value}
。要获得所需的内容,您必须创建一个对象,例如obj
,然后使用obj[field]=value
构建它。然后将其传递到put请求中,替换当前指定的与对象分解工作方式相关的{field,value}
{field,value}
实际上是指{field:field,value:value}
。要获得所需的内容,您必须创建一个对象,例如obj
,然后使用obj[field]=value
构建它。然后将其传递到put请求中,替换当前指定的{field,value}
与我的同事在这里所说的不同,您不需要创建新对象
对对象使用动态键表示法:
updateProperty(field: string, value: string): Observable<Property> {
return this.http.put<Property>(`${this.apiUrl}/property`, { [field]: value });
}
updateProperty(字段:字符串,值:字符串):可观察{
返回this.http.put(`${this.apirl}/property`,{[field]:value});
}
与我的同事在这里所说的不同,您不需要创建新对象 对对象使用动态键表示法:
updateProperty(field: string, value: string): Observable<Property> {
return this.http.put<Property>(`${this.apiUrl}/property`, { [field]: value });
}
updateProperty(字段:字符串,值:字符串):可观察{
返回this.http.put(`${this.apirl}/property`,{[field]:value});
}
你不必创建一个新对象,你可以像OP那样在一行中完成(请随意检查我的答案,@RobbieMills)nice@trichetriche我以前从未见过这种语法,谢谢你的分享。你不必创建一个新对象,你可以像OP那样在一行中完成(请随意检查我的答案,@RobbieMills)nice@trichetriche我以前从未见过这种语法,谢谢分享。