我有4個不同的數組,每個數組都有不同的長度。我需要弄清楚array.length以知道當前所選陣列中有多少個步驟。我希望這是在代碼中完成的,如果可能的話,不要在模板中完成。Angular2從可觀察到的數組大小
step.service.ts
public apiURL(){
if (this.heroID == 0){
this.stepsUrl = 'api/stepsLiftSensor';
} else if(this.heroID == 1){
this.stepsUrl = 'api/stepsBucketSensor';
} else if(this.heroID == 2){
this.stepsUrl = 'api/stepsEmptyBucketSensor';
} else if(this.heroID == 3) {
this.stepsUrl = 'api/stepsFullBucketSensor';
}
console.log(this.stepsUrl);
}
getSteps(): Observable<Step[]> {
return this.http.get(this.stepsUrl)
.map(response => response.json().data as Step[]);
}
getStep(id: number): Observable<Step> {
const url = `${this.stepsUrl}/${id}`;
return this.http.get(url)
.map(response => response.json().data as Step);
}
校準detail.component.ts
現在它經過11個步驟,但現在的數組大小不同會打破這樣的。我想這樣做(currentStep < this.stepService.getSteps()。length()),但這似乎不工作,因爲observable沒有length屬性。有沒有辦法做到這一點?
private currentStep: number = 0; //Variable for the current step
next() {
//Assuming there is another step it pulls the next step up, else it says "End of steps"
if (this.currentStep < 11) { //make sure dont go past number of steps
this.currentStep ++; //Increments the step
this.stepService.getStep(this.currentStep).subscribe(data => (this.mainStepText = data.name)); //Handles return and sets the data it contains to local variable
} else {
this.mainStepText = "Calibration Finished.";
}
}
內存,data.service.ts
import { Injectable } from '@angular/core';
import { InMemoryDbService } from 'angular-in-memory-web-api';
import { Hero } from './hero.class';
import { Step } from './step.class';
import { HeroService } from "./hero.service";
import { StepService } from "./step.service";
import { ActivatedRoute, Params } from "@angular/router";
import 'rxjs/Rx';
@Injectable()
export class InMemoryDataService implements InMemoryDbService {
hero: Hero;
step: Step;
private heroService: HeroService;
private stepService: StepService;
private route: ActivatedRoute;
private location: Location;
constructor() {}
ngOnInit(): void {
this.route.params
.switchMap((params: Params) => this.heroService.getHero(+params['id']))
.subscribe(hero => this.hero = hero);
}
createDb() {
let heroes = [
{id: 1, name: 'Lift Sensor', path: 'stepsLiftSensor'},
{id: 2, name: 'Bucket Sensor', path: 'stepsBucketSensor'},
{id: 3, name: 'Empty Bucket'},
{id: 4, name: 'Full Bucket'}
];
let stepsLiftSensor = [
{id: 1, name: 'test1'},
{id: 2, name: 'test2'},
{id: 3, name: 'test3'},
{id: 4, name: 'test4'},
{id: 5, name: 'test5'}
];
let stepsBucketSensor = [
{id: 1, name: 'test1'},
{id: 2, name: 'test2'},
{id: 3, name: 'test3'},
{id: 4, name: 'test4'},
{id: 5, name: 'test5'}
{id: 6, name: 'test6'},
{id: 7, name: 'test7'},
{id: 8, name: 'test8'},
{id: 9, name: 'test9'},
{id: 10, name: 'test10'}
];
let stepsEmptyBucketSensor = [
{id: 1, name: 'test1'},
{id: 2, name: 'test2'},
{id: 3, name: 'test3'},
{id: 4, name: 'test4'},
{id: 5, name: 'test5'},
{id: 6, name: 'test6'}
];
let stepsFullBucketSensor = [
{id: 1, name: 'test1'},
{id: 2, name: 'test2'},
{id: 3, name: 'test3'},
{id: 4, name: 'test4'},
{id: 5, name: 'test5'}
];
return { heroes, stepsLiftSensor, stepsBucketSensor, stepsEmptyBucketSensor, stepsFullBucketSensor };
}
}
step.class.ts
export class Step {
id: number;
name: string;
}
增加了兩個班級,這是你想看到什麼? – John
你有沒有得到它的工作? @約翰 –
是的感謝問,我添加了一個答案,我如何解決它。 – John