I have been working on creating dynamic components for my application, allowing them to be reusable in different parts of the program. At this point, I have successfully developed text inputs that can be dynamically added and customized using the componentFactory
within a form, and they are functioning perfectly.
Now, I am facing the challenge of developing fully dynamic buttons that can also be customized when inserted into the targeted view (similar to the text inputs in the form). While I have managed to make most aspects generic and functional, I am encountering an issue with making the (click)
function dynamic. I aim to specify the function to be triggered using the componentFactory
, but I seem to be stuck at implementing this feature.
In my search for solutions, I have not come across any resources that address my specific problem. Below is the code for the component I have created so far:
button.component.ts
import { Component, OnInit, ViewEncapsulation, Input } from '@angular/core';
import { FormGroup } from '@angular/forms';
@Component({
selector: 'app-button',
templateUrl: './button.component.html',
styleUrls: ['./button.component.scss'],
encapsulation: ViewEncapsulation.None
})
export class ButtonComponent implements OnInit {
@Input() group: FormGroup;
@Input() type: string;
@Input() description: string;
@Input() class: string;
@Input() data: string;
@Input() callFunction: string;
constructor() { }
ngOnInit() {
}
}
button.component.html
<div [formGroup]="group">
<button type="{{ type }}" class="{{ class }}" (click)="{{callFunction}}">{{ description }}</button>
</div>
The issue lies in the button.component.html file where the (click)
directive does not work as expected. It triggers the following error:
Parser Error: Got interpolation ({{}}) where expression was expected
While everything else is functioning correctly, the inability to make the button's click action entirely dynamic hinders its full customization. Unfortunately, I have not found a suitable resource to address this particular requirement.
EDIT: I have included the function responsible for importing the component into my view:
buildLoginButton(){
let data = {
type: "button",
class: "btn btn-primary px-4",
description: this.translate.transform("pages[login_page][login_form][buttons][login]"),
callFunction: "login()", //I have tried this.login() as well
group: this.userForm
}
const inputFactory = this.resolver.resolveComponentFactory(ButtonComponent);
const loginButton = this.login_button.createComponent(inputFactory);
loginButton.instance.group = data.group;
loginButton.instance.type = data.type;
loginButton.instance.class = data.class;
loginButton.instance.description = data.description;
loginButton.instance.callFunction = data.callFunction;
}