Child listens for parent event in Angular 2

前端 未结 4 1176
轮回少年
轮回少年 2020-12-07 16:10

In angular docs there is a topic about listening for child events from parents. That\'s fine. But my purpose is something reverse!. In my app there is an \'admin.component\'

4条回答
  •  孤城傲影
    2020-12-07 16:57

    A more bare bones approach might be possible here if I understand the question correctly. Assumptions --

    • OP has a save button in the parent component
    • The data that needs to be saved is in the child components
    • All other data that the child component might need can be accessed from services

    In the parent component

    
    
    

    And in the child ..

    prop1:boolean;
      @Input()
      set setProp(p: boolean) {
        // -- perform save function here
    }
    

    This simply sends the button click to the child component. From there the child component can save the data independently.

    EDIT: if data from the parent template also needs to be passed along with the button click, that is also possible with this approach. Let me know if that is the case and I will update the code samples.

提交回复
热议问题