Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays Angular2从可观测数据获取数组大小_Arrays_Angular_Typescript - Fatal编程技术网

Arrays Angular2从可观测数据获取数组大小

Arrays Angular2从可观测数据获取数组大小,arrays,angular,typescript,Arrays,Angular,Typescript,我有4个不同的数组,每个数组的长度不同。我需要计算array.length,才能知道当前选定数组中有多少步。我希望这是在代码中完成的,而不是在模板中,如果可能的话 step.service.ts public apiURL(){ if (this.heroID == 0){ this.stepsUrl = 'api/stepsLiftSensor'; } else if(this.heroID == 1){ this.stepsUrl = 'api/stepsBucketSe

我有4个不同的数组,每个数组的长度不同。我需要计算array.length,才能知道当前选定数组中有多少步。我希望这是在代码中完成的,而不是在模板中,如果可能的话

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);
  }
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;
}

很难理解你想要什么。但从我读了5遍后得到的信息来看,一旦获得了步骤,就应该存储长度,并在
next()
方法中使用该变量:

stepsLength: number = 0;

getSteps(): Observable<Step[]> {
   return this.http.get(this.stepsUrl)
           .map(response => response.json().data as Step[])
           .do(steps => this.stepsLength = steps.length);
}
stepsLength:number=0;
getSteps():可观察{
返回this.http.get(this.stepsUrl)
.map(response=>response.json().data作为步骤[])
.do(步长=>this.stepsLength=步长.length);
}

很难理解你想要什么。但从我读了5遍后得到的信息来看,一旦获得了步骤,就应该存储长度,并在
next()
方法中使用该变量:

stepsLength: number = 0;

getSteps(): Observable<Step[]> {
   return this.http.get(this.stepsUrl)
           .map(response => response.json().data as Step[])
           .do(steps => this.stepsLength = steps.length);
}
stepsLength:number=0;
getSteps():可观察{
返回this.http.get(this.stepsUrl)
.map(response=>response.json().data作为步骤[])
.do(步长=>this.stepsLength=步长.length);
}

您应该修改服务本身以返回数组的长度和数据(我们称之为
lengthFromService

您可以将该长度存储在.ts中的一个变量中(我们称之为
arrLength
),并对该变量进行检查

在下一个()函数中,可以执行如下操作:

this.stepService.getStep(this.currentStep).subscribe(data => {this.mainStepText = data.name; this.arrLength = data.lengthFromService});

如果您需要更多帮助,请发布服务的代码,或向服务提供
数据的任何函数的代码。

您应该修改服务本身以返回数组的长度和数据(我们称之为
lengthFromService

您可以将该长度存储在.ts中的一个变量中(我们称之为
arrLength
),并对该变量进行检查

在下一个()函数中,可以执行如下操作:

this.stepService.getStep(this.currentStep).subscribe(data => {this.mainStepText = data.name; this.arrLength = data.lengthFromService});

如果您需要更多帮助,请发布服务的代码,或向服务提供
数据的任何函数的代码。

在重新阅读您的答案并查看我的代码后,我最终解决了它,我只使用了

this.stepService.getSteps().subscribe(data => (this.stepsLength = data.length));

然后把它放到我的校准细节的构造函数中。component.ts.

在重新阅读你们的答案并查看我的代码后,我最终解决了它,我只是使用了

this.stepService.getSteps().subscribe(data => (this.stepsLength = data.length));

并将其放入构造函数中,以获取我的校准详细信息。component.ts.

我尝试过这样做,但当我将其传回时,它是未定义的。我调用:getArrayLength():number{返回this.stepsLength;}并将其存储在:private stepsLength:number=this.stepService.getArrayLength()中;我试着这么做,但当我把它传回去时,它还没有定义。我调用:getArrayLength():number{返回this.stepsLength;}并将其存储在:private stepsLength:number=this.stepService.getArrayLength()中;增加了两门课,这是你想看到的吗?是的,谢谢你的提问,我添加了一个关于我如何解决它的答案。增加了两门课,这是你想看到的吗?是的,谢谢你的提问,我添加了一个关于我如何解决它的答案。