新的Typescript並試圖讓我的頭。Typescript在函數中傳遞變量
以下是基本上生成三個隨機數的代碼,我使用這些從json中選擇隨機對象。
@Injectable()
export class TrackService {
public drawn :number[]=[];
constructor(private http: Http) {
}
public getRandomTrack(): Observable<Track[]> {
this.generateRandomNumbers();
console.log(this.drawn); //this.drawn is available here
return this.http
.get(API_ENDPOINT)
.map(this.extractRandomData);
}
public extractRandomData(res: Response) {
let body = res.json();
console.log(this.drawn); //undefined here
var randomTrack = [];
for(var i=0;i<this.drawn.length; i++){
randomTrack.push(body.results[this.drawn[i]]);
}
return randomTrack;
}
private generateRandomNumbers(){
var available = [];
for (var i = 1; i<= 55; i++) {
available.push(i);
}
for (var i = 0; i <3; i++) {
var random = Math.floor((Math.random() * available.length));
this.drawn.push(available[random]);
available.splice(random, 1);
}
return this.drawn;
}
你extractRandomData this.drawn看到裏面是不確定的,我怎麼打發this.drawn成函數,所以我可以訪問陣列。
請幫忙。
嗨Maximus的感謝!請原諒我的無知,這裏有什麼? – Spdexter
它只是一個將保存值的變量 –