Pass data from parent to nth child in Angular 5 [duplicate]












1
















This question already has an answer here:




  • Angular 6 multiple @input() level

    1 answer




lets assume I got a parent component which interacts with its child component like:
enter image description here



If this is the case, I can pass some data to the child using the following syntax:




parent.component.ts




// imports..      
selector: "app-parent",
templateURL: "./parent.component.html",
// ...
export class ParentComponent{
public statement= "I am your father"!;
}



parent.component.html




<div>
<h1>
My name is Darth Vader
</h1>
</div>
<app-child [luke] = "statement"></app-child> // child selector!



child.component.ts




// imports..      
selector: "app-child",
templateURL: `<h2>{{"Luke, " + luke}}</h2>`
// ...
export class TestComponent{
@Input() public luke;
}


The result should be:




Luke, I am your father!




Nothing special this far.



Now what if my child component contains another child and therefore acts as its parent?




child.component.html




<app-secondChild></app-secondChild> // selector of second.child.component.ts!



second.child.component.html




<app-thirdChild></app-thirdChild> // selector of third.child.component.ts!



third.child.component.html




<app-fourthChild></app-fourthChild> // selector of fourth.child.component.ts!


Basically, I want to pass data from the parent to the last child in order to activate some HTML-code.



I hope this wasn't explained too complicatedly.










share|improve this question













marked as duplicate by SiddAjmera, Community Nov 25 '18 at 16:51


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.




















    1
















    This question already has an answer here:




    • Angular 6 multiple @input() level

      1 answer




    lets assume I got a parent component which interacts with its child component like:
    enter image description here



    If this is the case, I can pass some data to the child using the following syntax:




    parent.component.ts




    // imports..      
    selector: "app-parent",
    templateURL: "./parent.component.html",
    // ...
    export class ParentComponent{
    public statement= "I am your father"!;
    }



    parent.component.html




    <div>
    <h1>
    My name is Darth Vader
    </h1>
    </div>
    <app-child [luke] = "statement"></app-child> // child selector!



    child.component.ts




    // imports..      
    selector: "app-child",
    templateURL: `<h2>{{"Luke, " + luke}}</h2>`
    // ...
    export class TestComponent{
    @Input() public luke;
    }


    The result should be:




    Luke, I am your father!




    Nothing special this far.



    Now what if my child component contains another child and therefore acts as its parent?




    child.component.html




    <app-secondChild></app-secondChild> // selector of second.child.component.ts!



    second.child.component.html




    <app-thirdChild></app-thirdChild> // selector of third.child.component.ts!



    third.child.component.html




    <app-fourthChild></app-fourthChild> // selector of fourth.child.component.ts!


    Basically, I want to pass data from the parent to the last child in order to activate some HTML-code.



    I hope this wasn't explained too complicatedly.










    share|improve this question













    marked as duplicate by SiddAjmera, Community Nov 25 '18 at 16:51


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















      1












      1








      1









      This question already has an answer here:




      • Angular 6 multiple @input() level

        1 answer




      lets assume I got a parent component which interacts with its child component like:
      enter image description here



      If this is the case, I can pass some data to the child using the following syntax:




      parent.component.ts




      // imports..      
      selector: "app-parent",
      templateURL: "./parent.component.html",
      // ...
      export class ParentComponent{
      public statement= "I am your father"!;
      }



      parent.component.html




      <div>
      <h1>
      My name is Darth Vader
      </h1>
      </div>
      <app-child [luke] = "statement"></app-child> // child selector!



      child.component.ts




      // imports..      
      selector: "app-child",
      templateURL: `<h2>{{"Luke, " + luke}}</h2>`
      // ...
      export class TestComponent{
      @Input() public luke;
      }


      The result should be:




      Luke, I am your father!




      Nothing special this far.



      Now what if my child component contains another child and therefore acts as its parent?




      child.component.html




      <app-secondChild></app-secondChild> // selector of second.child.component.ts!



      second.child.component.html




      <app-thirdChild></app-thirdChild> // selector of third.child.component.ts!



      third.child.component.html




      <app-fourthChild></app-fourthChild> // selector of fourth.child.component.ts!


      Basically, I want to pass data from the parent to the last child in order to activate some HTML-code.



      I hope this wasn't explained too complicatedly.










      share|improve this question















      This question already has an answer here:




      • Angular 6 multiple @input() level

        1 answer




      lets assume I got a parent component which interacts with its child component like:
      enter image description here



      If this is the case, I can pass some data to the child using the following syntax:




      parent.component.ts




      // imports..      
      selector: "app-parent",
      templateURL: "./parent.component.html",
      // ...
      export class ParentComponent{
      public statement= "I am your father"!;
      }



      parent.component.html




      <div>
      <h1>
      My name is Darth Vader
      </h1>
      </div>
      <app-child [luke] = "statement"></app-child> // child selector!



      child.component.ts




      // imports..      
      selector: "app-child",
      templateURL: `<h2>{{"Luke, " + luke}}</h2>`
      // ...
      export class TestComponent{
      @Input() public luke;
      }


      The result should be:




      Luke, I am your father!




      Nothing special this far.



      Now what if my child component contains another child and therefore acts as its parent?




      child.component.html




      <app-secondChild></app-secondChild> // selector of second.child.component.ts!



      second.child.component.html




      <app-thirdChild></app-thirdChild> // selector of third.child.component.ts!



      third.child.component.html




      <app-fourthChild></app-fourthChild> // selector of fourth.child.component.ts!


      Basically, I want to pass data from the parent to the last child in order to activate some HTML-code.



      I hope this wasn't explained too complicatedly.





      This question already has an answer here:




      • Angular 6 multiple @input() level

        1 answer








      angular typescript






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 16:27









      Big DudeBig Dude

      93319




      93319




      marked as duplicate by SiddAjmera, Community Nov 25 '18 at 16:51


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by SiddAjmera, Community Nov 25 '18 at 16:51


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          2 Answers
          2






          active

          oldest

          votes


















          1














          You can transfer value from parent to child using 3-different way





          1. Using input in parent .html file



            <app-child [user]="user"></app-child>


            and child.ts file



            export class ChildComponent implements OnInit{
            @Input() user: SocialUser;
            }



          2. Using Simple Storage, storage.service.ts



            public user: String = '';


            Now import this service in the module.ts file and In parent.ts import storage service



            constructor(public storageService: StorageService){}
            ngOnInit(){this.storageService.user = 'user_value';}


            In child.ts file



            constructor(public storageService: StorageService){}
            ngOnInit(){console.log(this.storageService.user);}



          3. Using Observable In storage.service.ts



            public user: Subject<any> = new BehaviorSubject<any>(null);


            Now import this service in the module.ts file and In parent.ts import storage service



            constructor(public storageService: StorageService){}
            ngOnInit(){this.storageService.user.next('user_value')}


            In child.ts file



            constructor(public storageService: StorageService){}

            ngOnInit(){
            this.storageService.user.subscribe(user=> {if(user) console.log(user)});
            }



          This solution may solve your problem.






          share|improve this answer
























          • Thanks! That helped me, too. Now, I just got to find a way to replace the HTML-code of a given div inside a child with the HTML-code of the service. I know I got to use something like document.getElementByID("replaceIt").innerHTML = "..." , but this is another topic. Thanks!

            – Big Dude
            Nov 25 '18 at 16:53



















          1














          You could pass the data from parent to child using an input parameter until the last child get it. If the components in the middle will not require this data you could take other approach. I have a couple of options:




          1. You could use a Service Class with a property that holds the data that must be shared.


          2. You could use other patterns that work with a shared data store, you may want to take a look at Redux and ngrx store: https://github.com/ngrx/store







          share|improve this answer






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            You can transfer value from parent to child using 3-different way





            1. Using input in parent .html file



              <app-child [user]="user"></app-child>


              and child.ts file



              export class ChildComponent implements OnInit{
              @Input() user: SocialUser;
              }



            2. Using Simple Storage, storage.service.ts



              public user: String = '';


              Now import this service in the module.ts file and In parent.ts import storage service



              constructor(public storageService: StorageService){}
              ngOnInit(){this.storageService.user = 'user_value';}


              In child.ts file



              constructor(public storageService: StorageService){}
              ngOnInit(){console.log(this.storageService.user);}



            3. Using Observable In storage.service.ts



              public user: Subject<any> = new BehaviorSubject<any>(null);


              Now import this service in the module.ts file and In parent.ts import storage service



              constructor(public storageService: StorageService){}
              ngOnInit(){this.storageService.user.next('user_value')}


              In child.ts file



              constructor(public storageService: StorageService){}

              ngOnInit(){
              this.storageService.user.subscribe(user=> {if(user) console.log(user)});
              }



            This solution may solve your problem.






            share|improve this answer
























            • Thanks! That helped me, too. Now, I just got to find a way to replace the HTML-code of a given div inside a child with the HTML-code of the service. I know I got to use something like document.getElementByID("replaceIt").innerHTML = "..." , but this is another topic. Thanks!

              – Big Dude
              Nov 25 '18 at 16:53
















            1














            You can transfer value from parent to child using 3-different way





            1. Using input in parent .html file



              <app-child [user]="user"></app-child>


              and child.ts file



              export class ChildComponent implements OnInit{
              @Input() user: SocialUser;
              }



            2. Using Simple Storage, storage.service.ts



              public user: String = '';


              Now import this service in the module.ts file and In parent.ts import storage service



              constructor(public storageService: StorageService){}
              ngOnInit(){this.storageService.user = 'user_value';}


              In child.ts file



              constructor(public storageService: StorageService){}
              ngOnInit(){console.log(this.storageService.user);}



            3. Using Observable In storage.service.ts



              public user: Subject<any> = new BehaviorSubject<any>(null);


              Now import this service in the module.ts file and In parent.ts import storage service



              constructor(public storageService: StorageService){}
              ngOnInit(){this.storageService.user.next('user_value')}


              In child.ts file



              constructor(public storageService: StorageService){}

              ngOnInit(){
              this.storageService.user.subscribe(user=> {if(user) console.log(user)});
              }



            This solution may solve your problem.






            share|improve this answer
























            • Thanks! That helped me, too. Now, I just got to find a way to replace the HTML-code of a given div inside a child with the HTML-code of the service. I know I got to use something like document.getElementByID("replaceIt").innerHTML = "..." , but this is another topic. Thanks!

              – Big Dude
              Nov 25 '18 at 16:53














            1












            1








            1







            You can transfer value from parent to child using 3-different way





            1. Using input in parent .html file



              <app-child [user]="user"></app-child>


              and child.ts file



              export class ChildComponent implements OnInit{
              @Input() user: SocialUser;
              }



            2. Using Simple Storage, storage.service.ts



              public user: String = '';


              Now import this service in the module.ts file and In parent.ts import storage service



              constructor(public storageService: StorageService){}
              ngOnInit(){this.storageService.user = 'user_value';}


              In child.ts file



              constructor(public storageService: StorageService){}
              ngOnInit(){console.log(this.storageService.user);}



            3. Using Observable In storage.service.ts



              public user: Subject<any> = new BehaviorSubject<any>(null);


              Now import this service in the module.ts file and In parent.ts import storage service



              constructor(public storageService: StorageService){}
              ngOnInit(){this.storageService.user.next('user_value')}


              In child.ts file



              constructor(public storageService: StorageService){}

              ngOnInit(){
              this.storageService.user.subscribe(user=> {if(user) console.log(user)});
              }



            This solution may solve your problem.






            share|improve this answer













            You can transfer value from parent to child using 3-different way





            1. Using input in parent .html file



              <app-child [user]="user"></app-child>


              and child.ts file



              export class ChildComponent implements OnInit{
              @Input() user: SocialUser;
              }



            2. Using Simple Storage, storage.service.ts



              public user: String = '';


              Now import this service in the module.ts file and In parent.ts import storage service



              constructor(public storageService: StorageService){}
              ngOnInit(){this.storageService.user = 'user_value';}


              In child.ts file



              constructor(public storageService: StorageService){}
              ngOnInit(){console.log(this.storageService.user);}



            3. Using Observable In storage.service.ts



              public user: Subject<any> = new BehaviorSubject<any>(null);


              Now import this service in the module.ts file and In parent.ts import storage service



              constructor(public storageService: StorageService){}
              ngOnInit(){this.storageService.user.next('user_value')}


              In child.ts file



              constructor(public storageService: StorageService){}

              ngOnInit(){
              this.storageService.user.subscribe(user=> {if(user) console.log(user)});
              }



            This solution may solve your problem.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 24 '18 at 3:56









            Biplab MalakarBiplab Malakar

            40837




            40837













            • Thanks! That helped me, too. Now, I just got to find a way to replace the HTML-code of a given div inside a child with the HTML-code of the service. I know I got to use something like document.getElementByID("replaceIt").innerHTML = "..." , but this is another topic. Thanks!

              – Big Dude
              Nov 25 '18 at 16:53



















            • Thanks! That helped me, too. Now, I just got to find a way to replace the HTML-code of a given div inside a child with the HTML-code of the service. I know I got to use something like document.getElementByID("replaceIt").innerHTML = "..." , but this is another topic. Thanks!

              – Big Dude
              Nov 25 '18 at 16:53

















            Thanks! That helped me, too. Now, I just got to find a way to replace the HTML-code of a given div inside a child with the HTML-code of the service. I know I got to use something like document.getElementByID("replaceIt").innerHTML = "..." , but this is another topic. Thanks!

            – Big Dude
            Nov 25 '18 at 16:53





            Thanks! That helped me, too. Now, I just got to find a way to replace the HTML-code of a given div inside a child with the HTML-code of the service. I know I got to use something like document.getElementByID("replaceIt").innerHTML = "..." , but this is another topic. Thanks!

            – Big Dude
            Nov 25 '18 at 16:53













            1














            You could pass the data from parent to child using an input parameter until the last child get it. If the components in the middle will not require this data you could take other approach. I have a couple of options:




            1. You could use a Service Class with a property that holds the data that must be shared.


            2. You could use other patterns that work with a shared data store, you may want to take a look at Redux and ngrx store: https://github.com/ngrx/store







            share|improve this answer




























              1














              You could pass the data from parent to child using an input parameter until the last child get it. If the components in the middle will not require this data you could take other approach. I have a couple of options:




              1. You could use a Service Class with a property that holds the data that must be shared.


              2. You could use other patterns that work with a shared data store, you may want to take a look at Redux and ngrx store: https://github.com/ngrx/store







              share|improve this answer


























                1












                1








                1







                You could pass the data from parent to child using an input parameter until the last child get it. If the components in the middle will not require this data you could take other approach. I have a couple of options:




                1. You could use a Service Class with a property that holds the data that must be shared.


                2. You could use other patterns that work with a shared data store, you may want to take a look at Redux and ngrx store: https://github.com/ngrx/store







                share|improve this answer













                You could pass the data from parent to child using an input parameter until the last child get it. If the components in the middle will not require this data you could take other approach. I have a couple of options:




                1. You could use a Service Class with a property that holds the data that must be shared.


                2. You could use other patterns that work with a shared data store, you may want to take a look at Redux and ngrx store: https://github.com/ngrx/store








                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 24 '18 at 3:11









                abarrenecheaabarrenechea

                393




                393