Angular 5:我的回复不会覆盖默认 属性
Angular 5: My response don't overwrite default property
我尝试在 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;" 我得到响应值但比我有反馈不能取消定义的错误。
欢迎提出任何建议。
谢谢。
一,构造函数没有显式定义这没关系,我猜
第二,如果在订阅之前未在 angular 服务中完成,则需要将响应映射到 json。
第三,
feedback: FeedbackStatistics = new FeedbackStatistics();
constructor(private profileService: ProfileService) {
feedback= new FeedbackStatistics();
}
这需要在构造函数中
我认为在最外层 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 使用 class,因为您没有添加任何方法。您可以使用这样的界面:
feedback.statistics.model.ts
export interface FeedbackStatistics {
overall: number;
technicalSkills: number;
communication: number;
commercial: number;
leadership: number;
}
我尝试在 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;" 我得到响应值但比我有反馈不能取消定义的错误。
欢迎提出任何建议。
谢谢。
一,构造函数没有显式定义这没关系,我猜
第二,如果在订阅之前未在 angular 服务中完成,则需要将响应映射到 json。
第三,
feedback: FeedbackStatistics = new FeedbackStatistics();
constructor(private profileService: ProfileService) {
feedback= new FeedbackStatistics();
}
这需要在构造函数中
我认为在最外层 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 使用 class,因为您没有添加任何方法。您可以使用这样的界面:
feedback.statistics.model.ts
export interface FeedbackStatistics {
overall: number;
technicalSkills: number;
communication: number;
commercial: number;
leadership: number;
}