Advertisement
andreadc

starRating

Mar 9th, 2020
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //COMPONENT
  2. export class StarRatingComponent implements OnInit {
  3.  
  4.   @Input() userId: number;
  5.  
  6.   // vecchio array che usavo prima del refactor
  7.   ratingArray:string[] = [];
  8.   // Observable da usare come array
  9.   ratingArray$ = new BehaviorSubject<string>(null);
  10.  
  11.   yellowStar:string = "/assets/images/yellow-star.jpg";
  12.   greyStar:string = "/assets/images/grey-star.jpg";
  13.  
  14.   subscription;
  15.  
  16.   constructor(private userService:UserService, public ratingService:RatingService) { }
  17.  
  18.   ngOnInit(): void {
  19.  
  20.     const observableRating:Observable<any> = this.ratingService.getRating(this.userId);
  21.    
  22.     this.subscription = observableRating
  23.       .pipe(
  24.         filter( data => data['count'] === 0)
  25.       ).subscribe({
  26.        
  27.         next:data => {
  28.  
  29.           for(let k=0;k<=4;k++){
  30.             if(condizione){
  31.                 //metodo attuale che mi da errore nel template
  32.                 this.ratingArray$.next(this.greyStar);
  33.                 //vecchio metodo prima del refactor
  34.                 this.ratingArray[k] = this.greyStar;
  35.             } else {
  36.                 //metodo attuale che mi da errore nel template
  37.                 this.ratingArray$.next(this.yellowStar);
  38.                 //vecchio metodo prima del refactor
  39.                 this.ratingArray[k] = this.yellowStar;
  40.             }
  41.           }
  42.  
  43.         }
  44.       });
  45.  
  46.   }
  47.  
  48. //TEMPLATE
  49. <div class="ratingStars" *ngFor="let rat of (ratingArray$ | async);let i = index">
  50.     <img src="{{rat}}">
  51. </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement