I'm new to Angular. I'm trying a simple thing today. I've gone through many answers but not able implement them correctly. I want to access some variables of filter-panel
in filter-bar
, (my two custom components). But none of the two is parent-child to each other. They're independent, though within the same directory. Here I've created a stackblitz. And here's my code:
filter-panel.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
...
})
export class FilterPanelComponent implements OnInit {
public activeFilters: string[];
public text: string="hello world";
constructor() {
this.activeFilters = [
'Apple',
'Grapes',
'Bana'
];
}
ngOnInit() {}
}
filter-bar.component.ts
import { Component, OnInit, ViewChild } from '@angular/core';
import { FilterPanelComponent } from './filter-panel/filter-panel.component';
@Component({
...
})
export class FilterBarComponent implements OnInit {
@ViewChild('FilterPanelComponent', {static : false}) filterPanel: FilterPanelComponent;
public values1: string[] = ['Philips'];
public values2: string[];
constructor() {
//this.values2=this.filterPanel.activeFilters;
}
ngOnInit() {
//console.log(this.values2);
}
}
After doing some more research I realized it's pointless to use @ViewChild
in this scenario. So I tried making a service. I tried @Input()
. I also tried this: How to use a variable from a component in another in Angular2. but I'm still not able to implement the solution. Please correct me.