我尝试在html视图中绑定数据,但是我的响应不会覆盖默认属性值。从API可以得到良好的响应。
feedback.statistics.model.ts
export class FeedbackStatistics {
overall: number = 0;
technicalSkills: number = 0;
communication: number = 0;
commercial: number = 0;
leadership: number = 0;
}
reviews.component.ts
export class ReviewsComponent implements OnInit {
message = '';
feedback: FeedbackStatistics = new FeedbackStatistics();
constructor(
private profileService: ProfileService
) { }
ngOnInit(): void {
this.profileService.getUserFeedbackStatistics().subscribe(
response => {
this.feedback = response;
},
error => {
this.message = error.error_description;
}
);
}
}
reviews.component.html
<!-- User Skills -->
<div class="d-flex flex-wrap text-center g-brd-around g-brd-gray-light-v4 g-pa-20 g-mb-40">
<div class="g-mr-40 g-mb-20 g-mb-0--xl" style="margin-left: 17px;">
<app-counter [from]=0 [to]=feedback.overall [of]=10 [animationTime]="700" [circleColor]="'#d3b6c6'" [fontSize]="80"></app-counter>
<h4 class="h6 g-font-weight-300 g-mt-5 mb-0">Overall</h4>
</div>
<div class="g-mr-40 g-mb-20 g-mb-0--xl">
<app-counter [from]=0 [to]=feedback.technicalSkills [of]=10 [animationTime]="700" [circleColor]="'#bee3f7'" [fontSize]="80"></app-counter>
<h4 class="h6 g-font-weight-300 g-mt-5 mb-0">Technical Skills</h4>
</div>
<div class="g-mr-40 g-mb-20 g-mb-0--xl">
<app-counter [from]=0 [to]=feedback.communication [of]=10 [animationTime]="700" [circleColor]="'#ffc2bb'" [fontSize]="80"></app-counter>
<h4 class="h6 g-font-weight-300 g-mt-5 mb-0">Communication</h4>
</div>
<div class="g-mr-40 g-mb-20 g-mb-0--xl">
<app-counter [from]=0 [to]=feedback.commercial [of]=10 [animationTime]="700" [circleColor]="'#c9ff97'" [fontSize]="80"></app-counter>
<h4 class="h6 g-font-weight-300 g-mt-5 mb-0">Commercial Acumen</h4>
</div>
<div class="g-mb-20 g-mb-0--lg">
<app-counter [from]=0 [to]=feedback.leadership [of]=10 [animationTime]="700" [circleColor]="'#eeeeee'" [fontSize]="80"></app-counter>
<h4 class="h6 g-font-weight-300 g-mt-5 mb-0">Leadership</h4>
</div>
</div> <!-- End User Skills -->
profile.service.ts
getUserFeedbackStatistics(): Observable<FeedbackStatistics> {
if (!JSON.parse(localStorage.getItem('authorizationData'))) {
return Observable.empty<FeedbackStatistics>();
}
return this.http.get<FeedbackStatistics>('api/reviews/feedbackStatistics?username=' + JSON.parse(localStorage.getItem('authorizationData')).userName)
.catch(error => this.handleError(error));
}
例如,如果我按代码中所述保留它,则对于所有属性,我将得到结果0,但是如果我这样声明:“ feedback:FeedbackStatistics;”我得到了响应值,但是比起我有一个错误,那就是反馈不能被定义。
任何建议都将受到欢迎。
谢谢。
答案 0 :(得分:0)
一个,没有明确定义构造函数,我想是可以的
第二,您需要将响应映射到json(如果在订阅之前没有在角度服务中完成)。
第三,
feedback: FeedbackStatistics = new FeedbackStatistics();
constructor(private profileService: ProfileService) {
feedback= new FeedbackStatistics();
}
这需要在构造函数中
答案 1 :(得分:0)
我认为在最外面的div上添加*ngIf="feedback"
应该可以解决以下问题:
reviews.component.ts
export class ReviewsComponent implements OnInit {
message = '';
feedback: FeedbackStatistics;
constructor(
private profileService: ProfileService
) { }
ngOnInit(): void {
this.profileService.getUserFeedbackStatistics().subscribe(
response => {
this.feedback = response;
},
error => {
this.message = error.error_description;
}
);
}
}
reviews.component.html
<!-- User Skills -->
<div *ngIf="feedback" class="d-flex flex-wrap text-center g-brd-around g-brd-gray-light-v4 g-pa-20 g-mb-40">
<div class="g-mr-40 g-mb-20 g-mb-0--xl" style="margin-left: 17px;">
<app-counter [from]=0 [to]=feedback.overall [of]=10 [animationTime]="700" [circleColor]="'#d3b6c6'" [fontSize]="80"></app-counter>
<h4 class="h6 g-font-weight-300 g-mt-5 mb-0">Overall</h4>
</div>
<div class="g-mr-40 g-mb-20 g-mb-0--xl">
<app-counter [from]=0 [to]=feedback.technicalSkills [of]=10 [animationTime]="700" [circleColor]="'#bee3f7'" [fontSize]="80"></app-counter>
<h4 class="h6 g-font-weight-300 g-mt-5 mb-0">Technical Skills</h4>
</div>
<div class="g-mr-40 g-mb-20 g-mb-0--xl">
<app-counter [from]=0 [to]=feedback.communication [of]=10 [animationTime]="700" [circleColor]="'#ffc2bb'" [fontSize]="80"></app-counter>
<h4 class="h6 g-font-weight-300 g-mt-5 mb-0">Communication</h4>
</div>
<div class="g-mr-40 g-mb-20 g-mb-0--xl">
<app-counter [from]=0 [to]=feedback.commercial [of]=10 [animationTime]="700" [circleColor]="'#c9ff97'" [fontSize]="80"></app-counter>
<h4 class="h6 g-font-weight-300 g-mt-5 mb-0">Commercial Acumen</h4>
</div>
<div class="g-mb-20 g-mb-0--lg">
<app-counter [from]=0 [to]=feedback.leadership [of]=10 [animationTime]="700" [circleColor]="'#eeeeee'" [fontSize]="80"></app-counter>
<h4 class="h6 g-font-weight-300 g-mt-5 mb-0">Leadership</h4>
</div>
</div> <!-- End User Skills -->
而且,由于您未添加任何方法,因此我不认为您应该对FeedbackStatistics使用类。您可以只使用这样的界面:
feedback.statistics.model.ts
export interface FeedbackStatistics {
overall: number;
technicalSkills: number;
communication: number;
commercial: number;
leadership: number;
}