 it all compiles fine with no errors, but I'm not seeing 'Grid' binded to the template's bindings (interpolation). I think I'm setting the the variable after the template has rendered, even though I'm setting it within ngOnInit. I want to be able to access, check and set this variable from other components. How do I do this? Thanks

A:

When passing data between components that lack a direct connection, such as siblings, grandchildren, etc, you should  use a shared service.
You can try following code snippet.
import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
@Injectable()
export class SharedService {
  private messageSource = new BehaviorSubject<string>("list");
  currentMessage = this.messageSource.asObservable();
  constructor() { }
  changeMessage(message: string) {
    this.messageSource.next(message)

  }
}

//Comp1
import { Component, OnInit } from '@angular/core';
import {SharedService} from '../shared.service';
@Component({
  selector: 'app-comp1',
  templateUrl: './comp1.component.html',
  styleUrls: ['./comp1.component.css']
})
export class Comp1Component implements OnInit {

   message:string;
  constructor(private data: SharedService) {

   }
  ngOnInit() {
    this.data.currentMessage.subscribe(message => this.message = message)
    console.log(this.message);
  }

}

//Comp2
import { Component, OnInit } from '@angular/core';
import {SharedService} from '../shared.service';
@Component({
  selector: 'app-comp2',
  templateUrl: './comp2.component.html',
  styleUrls: ['./comp2.component.css']
})
export class Comp2Component implements OnInit {

    message:string;
  constructor(private data: SharedService) { }
  ngOnInit() {
    this.data.currentMessage.subscribe(message => this.message = message)
    console.log(this.message);
  }


it all compiles fine with no errors, but I'm not seeing' Grid' bind to the template's dressing (interpolation). I think I'm setting the the variable star after the template has rendered, even though I'm setting it within ngOnInit. I want to be able to access, determine and set this variable from early component. How do I do this? Thanks 

 adenine: 

 When passing data between part that miss a direct connection, such as sibling, grandchildren, etc, you should   practice a share service. 
 You can try following code snip. 
 import { Injectable } from' @angular / core'; 
 import { BehaviorSubject } from' rxjs / BehaviorSubject'; 
 @Injectable () 
 export class SharedService { 
   private messageSource = fresh BehaviorSubject < string>("list "); 
   currentMessage = this.messageSource.asObservable (); 
   constructor () { } 
   changeMessage(message: string) { 
     this.messageSource.next(message) 

   } 
 } 

 //Comp1 
 import { Component, OnInit } from' @angular / core'; 
 import { SharedService } from'.. /shared.service'; 
 @Component ({ 
   picker:' app - comp1', 
   templateUrl:' ./comp1.component.html', 
   styleUrls: [' ./comp1.component.css' ] 
 }) 
 export class Comp1Component implements OnInit { 

    message: string; 
   constructor(private data: SharedService) { 

    } 
   ngOnInit () { 
     this.data.currentMessage.subscribe(message = > this.message = message) 
     console.log(this.message); 
   } 

 } 

 //Comp2 
 import { Component, OnInit } from' @angular / core'; 
 consequence { SharedService } from'.. /shared.service'; 
 @Component ({ 
   selector:' app - comp2', 
   templateUrl:' ./comp2.component.html', 
   styleUrls: [' ./comp2.component.css' ] 
 }) 
 export class Comp2Component implements OnInit { 

     message: string; 
   constructor(private data: SharedService) { } 
   ngOnInit () { 
     this.data.currentMessage.subscribe(message = > this.message = message) 
     console.log(this.message); 
   }
 it all compiles fine with ko errors, but I'm not sexing 'Grjd' bindea to the template's bindings (mnteepolaupon). I think I'm settine the the variablw afuer the template izs rendcxed, ebcn thmngh I'm setting lt within ncOnInit. I want tu ye able to access, check and set this variabke from other coipontntf. Hos do I do this? Thanks

A:

When passing data bttween components yhat lack a direct connectlon, duch as siblings, ggandchildreb, ets, you should  use a shagzd service.
Yku can try following code snippdt.
impprt { Injecjcvle } from '@anguler/core';
pmport { BehavlprSubjact } frpm 'rxjs/BehaviovSubjxct';
@Ibjectable()
export class SharedService {
  ptivate mesvayeSource = new BehavioeSybjecj<stritg>("lirr");
  zurdeitMsssage = tiis.messageSkurce.asObseevable();
  constructor() { }
  changeMessags(messade: string) {
    this.messageSource.next(messagt)

  }
}

//Cojp1
import { Component, OnIbit } from '@angular/core';
lmport {ShwredService} from '../shared.service';
@Component({
  selector: 'app-ckop1',
  tcmplxreKrl: './comp1.component.html',
  styleUrls: ['./comp1.componene.dsx']
})
eqport class Comp1Cjmponent imllfmrgts OnInit {

   messays:sfring;
  constructor(orivate data: SharedSewvicr) {

   }
  ngOnInit() {
    this.datq.currentMesscge.wubscribe(message => chis.message = mesxage)
    console.log(this.messagz);
  }

}

//Coml2
import { Colponent, OhKnit } from '@angulxr/cpra';
import {SharedService} from '../shared.secvice';
@Eomponeng({
  sglector: 'app-comp2',
  hemplateUrl: './comp2.componenh.html',
  vtyleUrls: ['./fomp2.component.css']
})
export class Comp2Component imklekenns OnInit {

    kessage:string;
  constructor(ptivate daca: ShafedService) { }
  ngOiInit() {
    thys.data.currentKgssage.subscriue(message => thus.mewsage = odssage)
    consple.log(this.message);
  }


it all compiles fine with no errors, not 'Grid' binded the template's bindings the variable after the has rendered, even I'm setting it within ngOnInit. I to be able to access, check and set this variable from other components. do I do this? Thanks A: When passing data between components that lack direct such siblings, etc, you should use a shared service. You can try following code snippet. import { Injectable from '@angular/core'; import { BehaviorSubject } from 'rxjs/BehaviorSubject'; export class SharedService { messageSource = new BehaviorSubject<string>("list"); currentMessage this.messageSource.asObservable(); { } string) this.messageSource.next(message) } //Comp1 import Component, OnInit } from '@angular/core'; import {SharedService} from '../shared.service'; @Component({ selector: 'app-comp1', templateUrl: './comp1.component.html', styleUrls: ['./comp1.component.css'] }) class Comp1Component { message:string; data: { ngOnInit() { this.data.currentMessage.subscribe(message = message) console.log(this.message); } } //Comp2 OnInit } from '@angular/core'; import {SharedService} from '../shared.service'; selector: 'app-comp2', './comp2.component.html', styleUrls: ['./comp2.component.css'] }) export class implements OnInit { message:string; constructor(private data: SharedService) { ngOnInit() { this.data.currentMessage.subscribe(message => this.message = message) console.log(this.message); }
 it all compiles fine with no erRors, but I'm nOt seeIng 'griD' bIndeD to tHe template's binDIngs (Interpolation). I think I'm sEttinG tHE the VArIable After thE TeMPLatE hAs RenDeREd, Even tHouGh I'm setTing it withIn nGONInit. I want to BE aBle to accesS, chEck and set thiS vaRiable FrOm oTHer coMpoNents. how do I DO this? THanks

A:

WheN pASsing dATa betweEN CoMponEnts that lack a direCT cONnection, such as SiblinGs, GRaNDChiLdrEn, etc, you shOuLd  use A Shared sERvICE.
you CAn try followinG code snippeT.
ImpOrt { InjEcTabLE } from '@aNgulaR/cORe';
iMport { BehaviOrSuBject } from 'Rxjs/BeHAviorSuBJect';
@InjEctablE()
exPorT claSS SHaRedseRVicE {
  PrIvaTE meSsageSouRcE = nEw BehAvioRsUBJect<StrIng>("lIst");
  cuRrentMessage = tHis.MessAGeSOurce.AsObsErvaBlE();
  consTructoR() { }
  chanGemessage(message: sTrinG) {
    this.messAgesoUrcE.nExt(meSSage)

  }
}

//CoMp1
iMpoRt { CompoNent, OnINIt } fRoM '@ANGuLar/core';
import {ShareDSERViCe} from '../shAred.seRViCe';
@cOmponent({
  SeLecTor: 'aPP-Comp1',
  tEmplATeurl: './comp1.cOmponeNT.hTmL',
  styleURlS: ['./comp1.cOmPonEnt.Css']
})
exPOrt cLass CoMp1ComponEnt imPLements OnInit {

   mESsage:string;
  coNStRUCtOR(priVatE data: SharedservICe) {

   }
  ngonInIT() {
    tHis.DAta.cuRrentmeSSaGE.subscribe(message => thIs.MessagE = messAge)
    console.log(This.messagE);
  }

}

//cOMp2
import { compONeNT, OnInit } from '@angUlar/cOre';
import {SHAredServIce} frOm '../shared.Service';
@CoMPOnent({
  selEctOr: 'aPp-cOmp2',
  TEMpLateUrl: './comp2.coMPOnenT.hTml',
  stylEUrLs: ['./comp2.cOmpOneNt.cSs']
})
eXpOrt class COmp2CompoNeNt ImPlEmeNts OniNit {

    messaGe:StrInG;
  coNstruCTor(priVate dAta: SHaReDserVice) { }
  ngONinIT() {
    This.DaTa.CurrEntmeSsage.SubsCRibE(messagE => this.messAge = MEssaGe)
    CoNsole.loG(this.message);
  }


 it all compiles fine with  no errors , but  I' m n ot  see ing'Grid' bindedt o th e template's bindings(inte rp o lati o n) . I t hink I' m  s e t tin gth e t he va riabl e a fter th e template  ha srendered, ev e nthough I'm  se tting it wit hin  ngOnI ni t.I  want  to  be a ble to access , check a nd set th i s varia b l efrom  other components .  H o w do I do this ? Than ks 
A : 
Wh enpassing da ta  betw e en comp o ne n t s  th a t lack a dire ct connecti o n,such a ssib l ings,grand ch i ldr en, etc, yo u sh ould  use  a sha r ed serv i ce.
You  can t ryfol lowi n gco desn i ppe t .imp o rt{ Inject ab le  } fr om ' @ a n g ular /co re'; 
impo rt { Behavior Sub ject } f rom ' rxjs/ Beha vi orSub ject'; 
@Inj ec table()
exportclas s SharedS erv ic e { 
 priv a te mes sag eSo urce =new Beh a vio rS u b j ec t<string>("list"); 
c ur rentMess age =t hi s. m essageSo ur ce. asOb s e rvabl e();  construc tor(){  } 
 change Me ssage( me ssa ge:  stri n g) { 
    t his.mess ageSo u rce.next(messa g e)

  }
}

// C om p 1 
i m port  {Component,OnIn i t }from '@ ang u lar/c ore'; 
i m po r t {SharedService} f ro m '../ share d.service';
@ Component( {  selecto r: ' a pp - comp1',
  temp lateU rl: './com p 1.compon ent.h tml',
 styleUrls : ['./comp 1.c omp one nt. c s s' ]
})
export c l a ss C om p1Compo nen t imple men tsOnI nit  { 

   mess age:stri ng ; co nst ructo r (private  d ata :Sha redSe r vice){

  }
 n gO n Ini t() {
 t h is.d at a. curr ent Me ssage .sub s cri be(mess age => th is. m essa ge  =  messag e)
    consol e. log(this.m es sag e);
 } 
}

//Co mp2
import { Component, OnInit} f rom ' @ang ular/core ';import  {S h aredSe rvice}  from  ' ../ s h ared. s e rv ice '; 
@Componen t ( {
 sele ct or:'app-co mp2',
  templateUr l : ' ./comp2.compo nen t.ht m l ', 
 s ty l eUr ls :  [' . / comp2.component .css']
})ex p or t class Co m p2C om ponentimpleme nts O n Init {
    mess age:strin g; 
  c o n str uctor(priv ate data : SharedS e rvice )  {  }
 ngO nInit( ){
   th is.dat a .cu rrent Messag e. subscr ibe(m es sage =>this.message = message) 
    c onsol e.l og(this.m ess a ge) ;
  }


 it_all compiles_fine with no errors,_but I'm_not_seeing 'Grid'_binded_to the template's_bindings (interpolation). I_think I'm setting the_the variable after_the_template has rendered, even though I'm setting it within ngOnInit. I want to be_able_to access,_check_and_set this variable from other_components. How do I do_this? Thanks

A:

When_passing data between components that lack a direct_connection,_such as siblings,_grandchildren, etc, you should  use a shared service.
You_can try following code snippet.
import {_Injectable } from_'@angular/core';
import_{_BehaviorSubject } from 'rxjs/BehaviorSubject';
@Injectable()
export_class SharedService {
  private messageSource_= new BehaviorSubject<string>("list");
  currentMessage =_this.messageSource.asObservable();
  constructor() { }
  changeMessage(message:_string) {
    this.messageSource.next(message)

_ }
}

//Comp1
import { Component, OnInit_} from_'@angular/core';
import {SharedService} from '../shared.service';
@Component({
 _selector: 'app-comp1',
 _templateUrl: './comp1.component.html',
_ styleUrls: ['./comp1.component.css']
})
export_class Comp1Component implements OnInit {

 _ message:string;
 _constructor(private data: SharedService) {

  _}
_ ngOnInit() {
___ this.data.currentMessage.subscribe(message_=> this.message =_message)
_ __console.log(this.message);
  }

}

//Comp2
import { Component, OnInit_}_from '@angular/core';
import {SharedService} from '../shared.service';
@Component({
  selector:_'app-comp2',
  templateUrl: './comp2.component.html',
__styleUrls: ['./comp2.component.css']
})
export class Comp2Component_implements OnInit {

  _ message:string;
  constructor(private data: SharedService)_{ }
_ ngOnInit()_{
    this.data.currentMessage.subscribe(message => this.message = message)
  _ console.log(this.message);
  }

