There are two ways to intercept the input property:
Using a setter:
export class AcknowledgementComponent {
private _period = "";
@Input('period')
set period(period:string) {
this._period = (period && period.toUpperCase()) || 'No input';
}
// Works with async operations. Emample:
// set period(period:string) {
// setTimeout(() => {
// this._period = (period && period.toUpperCase()) || 'No input';
// }, 5000);
// }
get period():string { return this._period; }
}
Using ngOnChanges
:
import { Component, Input, SimpleChanges } from '@angular/core';
...
export class AcknowledgementComponent {
@Input() period;
ngOnChanges(changes: {[ propKey: string ]: SimpleChanges}){
this.period = '';
for(let propName in changes) {
let changedProp = changes[propName];
let newValue:string = String(changedProp.currentValue);
this.period = newValue.toUpperCase();
// Works with async operations. Emample:
// setTimeout(() => {
// this.period = (newValue && newValue.toUpperCase()) || 'No input';
// }, 5000);
}
}
}
Both examples transform the input string
to uppercase.