列出Angular 2+中的订阅值

时间:2018-04-13 01:20:58

标签: angular typescript

我有一个连接到另一个兄弟组件的组件。从那时起,我提供了一项服务,以便进行这种沟通。到现在为止还挺好。问题是我无法检索组件内部的订阅值以便能够列出。

table.component.ts:

import { Component, OnInit } from '@angular/core';
import { SharedService } from './../service/shared.service';
import { Response } from '@angular/http';

@Component({
  selector: 'table-component',
  templateUrl: './table.component.html',
  styleUrls: ['./table.component.scss']
})

export class TableComponent implements OnInit {

  private value: any;
  private data: any;
  private filteredValues = [];
  private message: string = 'Utilize os filtros acima para exibir especialidades, ou';

  constructor(private service: SharedService) { }

  ngOnInit() {

    /* Get JSON results */
    this.service.getApi().subscribe(data => {
      this.data = data;      
    })

    /* Filter click button */
    this.service.currentValue.subscribe(value => {
      this.filteredValues = [];
      this.value = value;
      if(this.data !== undefined){
        for(let i = 0; i < this.data.length; i++){
          if(this.data[i].codeId === this.value[0].code && this.data[i].rowId === this.value[0].row){
            this.filteredValues.push(this.data[i]);
          }
        }
      }
    })
    //nothing appears on the console
    console.log('this.filteredValues', this.filteredValues);
  }

}

shared.service.ts:

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';

const URL = '../../assets/api.json';

@Injectable()
export class SharedService {

private messageSource = new BehaviorSubject<any>([]);
currentValue = this.messageSource.asObservable();

constructor(private http: HttpClient) { }

changeMessage(message: any) {
  this.messageSource.next(message)
}

getApi(){
  return this.http.get(URL);
}

}

我想使用这个filteredValues数组,但是,它没有显示任何内容。我知道它与订阅相关,但我不知道如何解决它。

0 个答案:

没有答案