angulartypescriptangular5angular-httpclientangular4-httpclient

Angular 5: My response don't overwrite default property


I try to bind data in my html view, but my response don't overwrite default property value. From the API I get good response.

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));
}

For example, if I leave it as stated in the code, I get it result 0 for all property, but if I declare like this: "feedback: FeedbackStatistics;" I get response value but than i have error that feedback can't be undefine.

Any proposal would be welcome.

Thank you.


Solution

  • I think adding a *ngIf="feedback" to the outermost div should fix the feedback can't be undefined problem like this: 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 -->
    

    And also, I don't think you should use a class for FeedbackStatistics since you don't add any methods. You could just use an inteface like this:

    feedback.statistics.model.ts

    export interface FeedbackStatistics {
      overall: number;
      technicalSkills: number;
      communication: number;
      commercial: number;
      leadership: number;
    }