提出请求Angular以更新整个对象

发布于 2025-01-27 07:14:49 字数 1793 浏览 5 评论 0原文

我试图做一个请求以更新大对象的请求。 该对象是这样结构化的: 预算

日期和百分比显示的日常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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫性小仙女 2025-02-03 07:14:49

您必须将整个更新的对象添加到看台上。

服务:

   UpdateBudget(id, updatedData): Observable<Budget> {
    const headers = new HttpHeaders().append('header', 'value');
    const params = new HttpParams().append('id', id);
    return this.http.put<Budget>(this.putUrl, updatedData, { headers, params });
  }

组件:

 //get budget by id
  getBudgetById(id) {
    this.UpdateDailyGoalService.getBudgetById(id).subscribe((data) => {
      this.budget = data;
      console.log('data' + data);
    });
  }

  updateBudget(id, updatedBudget) {
    this.UpdateDailyGoalService.UpdateBudget(id, updatedBudget).subscribe((data) => {
      this.budget.id = data.id;

      console.log('data id' + data.id);
    });
  }

You must add the whole updated object to the PUT.

Service:

   UpdateBudget(id, updatedData): Observable<Budget> {
    const headers = new HttpHeaders().append('header', 'value');
    const params = new HttpParams().append('id', id);
    return this.http.put<Budget>(this.putUrl, updatedData, { headers, params });
  }

Component:

 //get budget by id
  getBudgetById(id) {
    this.UpdateDailyGoalService.getBudgetById(id).subscribe((data) => {
      this.budget = data;
      console.log('data' + data);
    });
  }

  updateBudget(id, updatedBudget) {
    this.UpdateDailyGoalService.UpdateBudget(id, updatedBudget).subscribe((data) => {
      this.budget.id = data.id;

      console.log('data id' + data.id);
    });
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文