如何解决您现在如何在函数中更新json? API,角度
所以我有以下JSON:
.json
"type": [ {
"id": 2,"secondid": "1","name": "f","positionX": 0,"positionY": 0
}]
和以下服务:
public updateposition(Model: typemodel): Observable<any> {
return this.http.post(this.apiEndPoint + '/type' + '/' + typemodel.id,typemodel);
}
和以下TS:
.ts
x: number;
y: number;
updateposition()
{
}
因此,目标是通过单击带有html的按钮来更新json对象“类型”。 html部分没有问题。但是我不知道如何用在ts中声明的两个新位置x和y更新json对象。我想要在函数updateposition()中进行处理。您有什么建议吗:)?
解决方法
您将必须使用Dependency Injection获取对组件中Service
的引用,并使用它来检索和存储JSON对象。然后像单击该按钮时在JS中的其他任何对象一样对其进行更新,并进行API调用。请务必订阅结果,否则API调用将无法进行。
// ...
export class MyComponent {
public jsonObject: TypeModel = { ... };
public constructor(private service: Service) { }
// ...
public function updateposition() {
this.jsonObject[0].positionX = 52;
this.jsonObject[0].positionY = 42;
this.jsonObject = this.service.updateposition(this.jsonObject).subscribe(
result => {
console.log("The API call returned",result);
}
);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。