提出请求Angular以更新整个对象
我试图做一个请求以更新大对象的请求。 该对象是这样结构化的: 预算
日期和百分比显示的日常goal和百分比显示像这样
用户可以更改输入的百分比,我需要PUT请求来更新整个对象,一旦用户单击“”保存“按钮。
我的service.ts
export class UpdateDailyGoalService {
constructor(
private http: HttpClient,
private StartDateService: StartDateService
) {}
startDate = this.StartDateService.getStartDate();
year = this.startDate.getFullYear();
budget: Budget;
putUrl = 'http://localhost:8080/budgets/{id}';
getUrl = 'http://localhost:8080/budgets/search';
getBudgetById(id): Observable<Budget> {
const headers = new HttpHeaders().append('header', 'value');
const params = new HttpParams().append('id', id);
return this.http.get<Budget>(this.getUrl, { headers, params });
}
UpdateBudget(id): Observable<Budget> {
const headers = new HttpHeaders().append('header', 'value');
const params = new HttpParams().append('id', id);
return this.http.put<Budget>(this.putUrl, { headers, params });
}
}
我的component.ts
//get budget by id
getBudgetById(id) {
this.UpdateDailyGoalService.getBudgetById(id).subscribe((data) => {
this.budget = data;
console.log('data' + data);
});
}
//put request dailygoals into
updateBudget(id) {
// const data = JSON.stringify(this.budget.id);
this.UpdateDailyGoalService.UpdateBudget(id).subscribe((data) => {
this.budget.id = data.id;
console.log('data id' + data.id);
});
}
我不知道功能有什么问题,但它不起作用。 我希望你能帮我,谢谢
I trying to do a put request to update a big object.
The object is structured like this:
Budget
The dailygoal with the date and the percentage is displayed like this
The user can change the percentage of the inputs and I need the put request to update the whole object once the user click on the "save" button.
my service.ts
export class UpdateDailyGoalService {
constructor(
private http: HttpClient,
private StartDateService: StartDateService
) {}
startDate = this.StartDateService.getStartDate();
year = this.startDate.getFullYear();
budget: Budget;
putUrl = 'http://localhost:8080/budgets/{id}';
getUrl = 'http://localhost:8080/budgets/search';
getBudgetById(id): Observable<Budget> {
const headers = new HttpHeaders().append('header', 'value');
const params = new HttpParams().append('id', id);
return this.http.get<Budget>(this.getUrl, { headers, params });
}
UpdateBudget(id): Observable<Budget> {
const headers = new HttpHeaders().append('header', 'value');
const params = new HttpParams().append('id', id);
return this.http.put<Budget>(this.putUrl, { headers, params });
}
}
my component.ts
//get budget by id
getBudgetById(id) {
this.UpdateDailyGoalService.getBudgetById(id).subscribe((data) => {
this.budget = data;
console.log('data' + data);
});
}
//put request dailygoals into
updateBudget(id) {
// const data = JSON.stringify(this.budget.id);
this.UpdateDailyGoalService.UpdateBudget(id).subscribe((data) => {
this.budget.id = data.id;
console.log('data id' + data.id);
});
}
I don't know what s wrong with the functions but it doesn't work.
I hope you can help me, thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须将整个更新的对象添加到看台上。
服务:
组件:
You must add the whole updated object to the PUT.
Service:
Component: