1
I have a component that connects with another sister component. From that, I made a service so that it is possible to make this communication. So far so good. The problem is that I can’t recover the values of subscribe that is inside a component to be able to list.
table.componentts.:
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){
console.log('value',this.value)
console.log('data', this.data)
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]);
}
}
console.log(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);
}
}
I wanted to consume this array filteredValues
, however, it doesn’t show me anything. I know it’s related to subscribe, but I have no idea how to fix it.