Adding two numbers in Angular with @Input
Problem: You want a tag which takes two parameters and return the sum on the screen. Something like:
<sum first="5" second="4"/>
Solution:
That's it! 😃
sum.component.ts:
import {Component, Input} from '@angular/core';
@Component({
selector: 'sum',
standalone: true,
imports: [],
templateUrl: './sum.component.html',
styleUrl: './sum.component.css'
})
export class SumComponent {
@Input() first = '';
@Input() second = '';
sum(): Number {
return Number.parseInt(this.first) + Number.parseInt(this.second)
}
}
sum.component.html:
{{sum()}}