如何使用get从httpclient URL中推出数组中的数据?
我仍然是编程的新手,如果问题可能很愚蠢,我深表歉意。
但是我想知道如何在数组中保存“ get(url)”数据。 此数据在数组中,我想执行数组的长度
一旦 检索数组的长度,我将制作一种随机值以显示随机面的方法
。
import { Observable, Subject, Subscription } from 'rxjs';
import { FaceSnap } from './../models/face-snap.model';
import { Injectable } from "@angular/core";
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root' //on renregistre ce service à la racine de l'application
})
export class FaceSnapService {
// newFaceSnap$ = new Observable<FaceSnap[]>();
constructor(private http:HttpClient){
}
getAllFaceSnaps(): Observable<FaceSnap[]>{
// return this.faceSnaps;
return this.http.get<FaceSnap[]>('http://localhost:3000/facesnaps');
}
getFaceSnapById(faceSnapId: number): Observable<FaceSnap>{
// const faceSnap = this.faceSnaps.find(faceSnap => faceSnap.id===faceSnapId)
return this.http.get<FaceSnap>(`http://localhost:3000/facesnaps/${faceSnapId}`);
}
showOneFaceSnap(){
const faceTab = [];
this.http.get<FaceSnap[]>('http://localhost:3000/facesnaps').subscribe(facesnaps=>
facesnaps.forEach(data=> faceTab.push(data)) );
console.log("array ",faceTab, "array length ", faceTab.length );
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
I'm still a newbie in programming and I apologize if the question may be stupid.
But i want to know how i can save data from a "get(url)" in an array. Once this data is in an array, I want to do the length of the array but when i do a "console.log to check the values and the length with "array.length" it always show "0".
In fact,after retrieving the length of the array, I would make a method to retrieve a random value to display a random faceSnap.
My problem is in the function : showOneFaceSnap()
import { Observable, Subject, Subscription } from 'rxjs';
import { FaceSnap } from './../models/face-snap.model';
import { Injectable } from "@angular/core";
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root' //on renregistre ce service à la racine de l'application
})
export class FaceSnapService {
// newFaceSnap$ = new Observable<FaceSnap[]>();
constructor(private http:HttpClient){
}
getAllFaceSnaps(): Observable<FaceSnap[]>{
// return this.faceSnaps;
return this.http.get<FaceSnap[]>('http://localhost:3000/facesnaps');
}
getFaceSnapById(faceSnapId: number): Observable<FaceSnap>{
// const faceSnap = this.faceSnaps.find(faceSnap => faceSnap.id===faceSnapId)
return this.http.get<FaceSnap>(`http://localhost:3000/facesnaps/${faceSnapId}`);
}
showOneFaceSnap(){
const faceTab = [];
this.http.get<FaceSnap[]>('http://localhost:3000/facesnaps').subscribe(facesnaps=>
facesnaps.forEach(data=> faceTab.push(data)) );
console.log("array ",faceTab, "array length ", faceTab.length );
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
AS rsd 提到,数据可在订阅中可用
As RSD mentioned, data is available inside the subscription