Próbuję zwrócić jedną losową wartość z tablicy, ale coś nie wychodzi. Jakieś sugestie ?
Array:
[ { "id": "1", "category": "comedy", "content": "Millers" }, { "id": "2", "category": "horror", "content": "Sinister" }]
HTML:
<div class="container">
<p *ngFor="let movie of movies"> {{ movie.content }}</p> </div>
</div>
<button class="next">
Next
</button>
Service:
loadData(): Observable<any> {
return this.http.get<any>(`http://localhost:3004/movies`).pipe(
map(response => {
return response;
})
)
};
Component.ts:
export class HomeComponent implements OnInit {
public movies:any = [];
constructor(private PagesService: PagesService
) { }
ngOnInit(): void {
this.loadData()
};
loadData() {
this.PagesService.loadData().subscribe(response => {
console.log(response)
this.movies[Math.floor(Math.random() * this.movies.length)];
//this.movies = response
})
};
}
Proóbowałam tez coś takiego :
<!-- <p>{{ randomValue }}</p>-->
ts.:
// var randomValue = this.movies[Math.floor(Math.random() * this.movies.length)];
// return randomValue