1

Angular 2 newbie here, I am trying to make master form that handles basic activities like resetting, save functionalities. The Master Form uses ng-content to load its content area.

On click of save button I want Master Form to call child components save() function before it executes its own set of instructions.

How would I do that?

I think this question is somewhat similar to what I want but the solution given there is not working Angular 2 call function of component that was inserted with transclusion (ng-content)

Below is my code, requesting you to help

form.component.ts

import { Component, Input, Output, ContentChild } from '@angular/core';

@Component({
  selector: 'app-form',
  templateUrl: './form.component.html',
  styleUrls: ['./form.component.css']
})
export class FormComponent{
  @Input() headerTitle:string = "Header Title";
  @Input() saveBtnText: string = "Save";
  @Input() resetBtnText: string = "Reset";
  @Input() addBtnText: string = "Add";
  @ContentChild('transcludedContent') contentChild;

  saveForm(){
    console.log("Parent Form save");
    console.log(this.contentChild.saveForm1());
  }
}

test1.component.html

    <app-form headerTitle="my header" #transcludedContent>
        <div form-body>
            <p>
             Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, 
            </p>
        </div>
    </app-form>

test1.component.ts

import { Component, OnInit } from '@angular/core';
import { FormComponent } from '../form/form.component';

@Component({
  selector: 'app-test1',
  templateUrl: './test1.component.html',
  styleUrls: ['./test1.component.css']
})
export class Test1Component extends FormComponent {
  saveForm1(){
    console.log("Child Form save");
  }
}

I am unable to get reference of test1.component in form.component so that I can call its save() function. What would be the right way to achieve this ?

Edit 1:

Also adding form.component.html for future references

<div class="panel panel-default fcs-form">
  <div class="panel-header form-header">
        {{headerTitle}}
  </div>

  <div class="panel-body form-body">
    <ng-content select="[form-body]"></ng-content>
  </div>
  <div class="panel-footer text-center form-footer">
        <button class="btn btn-primary">{{resetBtnText}}</button>
        <button class="btn btn-primary">{{saveBtnText}}</button>
        <button class="btn btn-primary">{{addBtnText}}</button>
  </div>
</div>
Community
  • 1
  • 1
Flying Gambit
  • 1,238
  • 1
  • 15
  • 32

2 Answers2

2

I think this is what you want - communicating from child to parent using an output event:

@Component({
  selector: 'app-form',
  templateUrl: './form.component.html',
  styleUrls: ['./form.component.css']
})
export class FormComponent{
  @Input() headerTitle:string = "Header Title";
  @Input() saveBtnText: string = "Save";
  @Input() resetBtnText: string = "Reset";
  @Input() addBtnText: string = "Add";
  @Output() save:EventEmitter = new EventEmitter();

  saveForm(){
    console.log("Parent Form save");
    this.save.emit(null);
  }
}
<app-form headerTitle="my header" (save)="saveForm()">
    <div form-body>
        <p>
         Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, 
        </p>
    </div>
</app-form>
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
1

If you want to keep the ContentChild approach, then this should work. But I recommend using an @Input and @Output approach, like Günter Zöchbauers answer.

@ContentChild('transcludedContent') contentChild: any;

saveForm(){
  if(typeof this.child.save === 'function') { //make sure that the save method exists
    this.child.save();
  }
}

I also updated the plunker for you, to demonstrate the concept https://plnkr.co/edit/0T0BBEJm73NWxUtW9Oje?p=preview

Community
  • 1
  • 1
Fredrik Lundin
  • 8,006
  • 1
  • 29
  • 35