I have two components on one page: Component1 and Component2. Inside each of those is Component3. Obviously each Component 3 is its own instantiation of the component. However, I would like a global variable between the two. I'm creating a side-by-side comparison of some data, and would like an accordion to work, so when I click to expand the accordion on one Component 3, the other one also opens. I have been searching for hours and cannot find a resolution to this.
What I want is, for instance:
(click) = "changeGlobalVar()"
to change the global variable. Then I would like to have
*ngIf="globalVar"
That way, the ngIf works on both Component 3, no matter which one I click on.
Could someone please help me out? I've been searching for an answer to this for hours.
Here's what my service code looks like but doesn't seem to be working:
import {Injectable} from '@angular/core';
@Injectable()
export class DropDownService {
public _acDropDownToggle: boolean;
setValue(val) {
this._acDropDownToggle = val;
}
getValue() {
return this._acDropDownToggle;
}
}
serviceand inject it in the components. They are singletons so the value will be the same across components.providersarray in the component that is above Component1 and Component2. Then each Component3 instance should get a reference to the same/single instance of your service.