Is there any option for how to pass only value from one variable saved in Component1 to a variable in Component2 without any template binding?
I have component Header and Footer, and in the header is a variable test which is boolean and I need pass value from variable TEST to variable TEST2 in Footer component.
I was looking for some solutions with @Input but I did not find any solutions without template bindings as [test]="test"
In short, I just need to pass value from one .ts file to another .ts file.
I was following this example: Pass data to nth level child component in Angular 4
But variable still is not passed to the component
HeaderService.ts
import { Injectable } from '@angular/core';
@Injectable()
export class HeaderService {
getTheme: boolean;
}
HeaderComponent.ts
import { Component, OnInit, ViewEncapsulation } from '@angular/core';
import { SideBarService } from '../sidebar/sidebar.service';
import { googleMapConfig } from '../content/google-map/config.service';
import { HeaderService } from './header.service';
@Component({
selector: 'app-header',
templateUrl: './header.component.html',
styleUrls: ['./header.component.scss'],
encapsulation: ViewEncapsulation.None,
})
export class HeaderComponent implements OnInit {
Name = 'Menučkáreň';
Navigation = 'Navigácia';
nightTheme;
icons = ['favorites','notification_important'];
tooltip = ['Obľúbené položky','Zoznam zmien'];
constructor(
public sideBarService: SideBarService,
public mapService: googleMapConfig,
private headerService: HeaderService
) { }
public toggle() {
this.sideBarService.toggle.emit();
}
public changeDark() {
this.nightTheme = true;
this.headerService.getTheme = true;
this.mapService.changeDark.emit();
}
public changeLight() {
this.nightTheme = false;
this.headerService.getTheme = false;
this.mapService.changeLight.emit();
}
ngOnInit() { }
}
FooterComponent
import { Component } from '@angular/core';
import { HeaderService } from '../header/header.service';
@Component({
selector: 'app-footer',
templateUrl: './footer.component.html',
styleUrls: ['./footer.component.scss'],
providers: [HeaderService]
})
export class FooterComponent {
Copyright = 'Copyright 2018 | Vytvoril Patrik Spišák';
Version = '0.0.1';
nightTheme: boolean;
constructor(private headerService: HeaderService) {
this.nightTheme = this.headerService.getTheme;
}
}
So when I invoke my function changeDark() from HeaderComponent.html it does not trigger this.headerService.getTheme = true;
<mat-grid-tile>
<button (click)="this.changeDark($event)" mat-icon-button>
<mat-icon aria-label="Nočný režim">brightness_3</mat-icon>
</button>
</mat-grid-tile>
UPDATE
So I was able achieve what I was needed with this code: Problem was with providers declared in FooterComponent. While were providers declared in FootersComponent I was getting Undefined, when I removed them and keep providers only in app.modules.ts variable is set and read properly.
HeaderService.ts
import { Injectable } from '@angular/core';
@Injectable()
export class HeaderService {
nightTheme:boolean;
get data():boolean{
return this.nightTheme;
}
set data(value:boolean){
this.nightTheme = value;
}
constructor(){}
}
Header Component
import { Component, OnInit, ViewEncapsulation } from '@angular/core';
import { SideBarService } from '../sidebar/sidebar.service';
import { googleMapConfig } from '../content/google-map/config.service';
import { HeaderService } from './header.service';
@Component({
selector: 'app-header',
templateUrl: './header.component.html',
styleUrls: ['./header.component.scss'],
encapsulation: ViewEncapsulation.None,
})
export class HeaderComponent implements OnInit {
Name = 'Menučkáreň';
Navigation = 'Navigácia';
icons = ['favorites','notification_important'];
tooltip = ['Obľúbené položky','Zoznam zmien'];
constructor(
public sideBarService: SideBarService,
public mapService: googleMapConfig,
private headerService: HeaderService
) {}
public toggle() {
this.sideBarService.toggle.emit();
}
public changeDark() {
this.headerService.nightTheme = true;
console.log(this.headerService.nightTheme);
this.mapService.changeDark.emit();
}
public changeLight() {
this.headerService.nightTheme = false;
console.log(this.headerService.nightTheme);
this.mapService.changeLight.emit();
}
ngOnInit() { }
}
Footer Component
import { Component, OnInit } from '@angular/core';
import { HeaderService } from '../header/header.service';
@Component({
selector: 'app-footer',
templateUrl: './footer.component.html',
styleUrls: ['./footer.component.scss'],
})
export class FooterComponent {
Copyright = 'Copyright 2018 | Vytvoril Patrik Spišák';
Version = '0.0.1';
constructor(private headerService: HeaderService) { }
ngOnInit() {
console.log('FOOTER:'+this.headerService.nightTheme);
}
}