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.componen
t 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>