I am trying to create a component in Angular 5 that will house a reusable template for a button. In different parts of my app buttons will call different functions, so I would like to be able to tell the given instance of the button what function to call. I know I could create an HTML tag for a button wherever I need it, but I was hoping I could create a reusable component so I can ensure formatting is consistent throughout the app.
Error
Got interpolation ({{}}) where expression was expected at column 0 in
[{{functioncall}}]
Component
<div id = "button">
<button type="button" class= "btn" (click) ="{{functioncall}}" >{{label}}</button>
</div>
And HTML
import { Component, OnInit, Input } from '@angular/core';
@Component({
selector: 'app-button',
templateUrl: './button.component.html',
styleUrls: ['./button.component.css']
})
export class ButtonComponent implements OnInit {
@Input() label:string;
@Input() functionCall:string;
constructor() { }
ngOnInit() {
}
}
you have to use the @Output
decorator to emit some event (from child to parent)
button.component.ts:
@Input() label: string;
@Output() onClick = new EventEmitter<any>();
onClickButton(event) {
this.onClick.emit(event);
}
button.component.html:
<div id = "button">
<button type="button" class= "btn" (click)="onClickbutton($event)" >{{label}}</button>
</div>
parent.component.ts
label = "button label"
functioncall(event) {
console.log('functioncall', event);
}
parent.component.html
<app-button (onClick)="functioncall($event)" [label]="label"></app-button>
See example: https://stackblitz.com/edit/angular-gghsax