【问题标题】:Angular getter and setter behaviour difference in post request jsonpost请求json中的角度getter和setter行为差异
【发布时间】:2020-06-05 11:33:34
【问题描述】:

这是一个 Angular 9 + ASP.NET Core 3.1 应用程序。 这是我使用的模型,我的问题依赖于此。

export class HeadingModel {
id: number;

private colourVal: string;
public get colour() {
    //code
}
public set colour(value) {
    //code
}
.
.
.etc

}

在左侧,您会看到在后端创建实体的发布请求的 JSON 请求正文。 在右侧,您会看到用于更新后端实体的 put 请求的 JSON 请求正文。 (所以我发送了一个 get 请求,对接收到的模型进行一些更改并发送回来)

我的问题是 但是在创建请求时,角度使用我的私有 var 名称而不是使用 getter 的名称。 (colorVal)

但在 post 请求中,它使用 getter 的名称,这是正确的。

为什么会有不同的行为?我该如何解决?

有我的组件代码

createHeading(): void {
this.requestManagerService.create
  (this.headingWrapperModel, '/iot/Headings/Add').subscribe(
    (res) => {
      if (res) {
        this.toastrService.success('Heading created successfully', '', {
          disableTimeOut: false,
        });
        this.resetForm();
        this.showSpinner = false;
      } else {
        this.toastrService.error('An error occurred while creating the heading.', '', {
          disableTimeOut: false,
        });
      }
    },
    (error) => {
      console.error();
      this.showSpinner = false;
      this.toastrService.error(
        'An error occurred while creating the heading. Please contact administrator.',
        '',
        { disableTimeOut: false }
      );
    }
  );

}

updateHeading(): void {
    this.requestManagerService.update
      (this.headingWrapperModel, '/iot/Headings/Update').subscribe(
        (res) => {
          if (res) {
            this.toastrService.success('Heading updated successfully', '', {
              disableTimeOut: false,
            });
            this.resetForm();
            this.showSpinner = false;
          } else {
            this.toastrService.error('An error occurred while updating the heading.', '', {
              disableTimeOut: false,
            });
          }
        },
        (error) => {
          console.error();
          this.showSpinner = false;
          this.toastrService.error(
            'An error occurred while updating the heading. Please contact administrator.',
            '',
            { disableTimeOut: false }
          );
        }
      );
  }

这是我的服务代码

public create<T>(item: T, endpoint: string): Observable<T> {
return this.httpClient.post<T>(environment.boardpacAPIURL + endpoint, item).pipe(
  catchError(this.handleError<T>('RestApiHttpService')));

}

public update<T>(item: T, endpoint: string): Observable<T> {
return this.httpClient.put<T>(environment.boardpacAPIURL + endpoint, item).pipe(
  catchError(this.handleError<T>('RestApiHttpService')));

}

【问题讨论】:

  • @R. Richard 如果您使用具有 getter 和 setter 的模型发送发布请求,您可以复制它。不需要进一步的调试信息。
  • 至少给我们帖子实际调用的代码。
  • 嗨,我已经更新了这个问题的更多细节。我希望这可以帮助你帮助我。非常感谢。
  • 我可以肯定地说,该错误不在发布的代码中,您必须查看this.headingWrapperModel 的设置位置。

标签: angular typescript angular9


【解决方案1】:

我认为您发送的不是同一个对象。在更新的情况下,你可能会用查询的结果覆盖你的对象,所以这个对象没有getter或setter,是get请求的结果。

当您更改值时,您不会直接更改属性。

在帖子的情况下,您仍然拥有具有 getter 和 setter 的对象,并且当您分配一个值时,您正在提供 _varible。

【讨论】:

    猜你喜欢
    • 2019-04-19
    • 2016-12-17
    • 1970-01-01
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    • 2021-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多