【发布时间】: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