string interpolation angular typescript code example

Example 1: typescript string interpolation

//Works only with ES6/ES2015 and above
let playerName:string = "Sachin Tendulkar";    
console.log(`${playerName} is the greatest cricketer of all time`)
 
//**** Output ****
//Sachin Tendulkar is the greates cricker of all time

Example 2: use javascript function in string interpolation angular

//angular HTML
{{secondsToHms(20)}}

//typescript
secondsToHms(d) {
    d = Number(d);
    var h = Math.floor(d / 3600);
    var m = Math.floor(d % 3600 / 60);
    var s = Math.floor(d % 3600 % 60);

    var hDisplay = h > 0 ? h + (h == 1 ? " hour, " : " hours, ") : "";
    var mDisplay = m > 0 ? m + (m == 1 ? " minute, " : " minutes, ") : "";
    var sDisplay = s > 0 ? s + (s == 1 ? " second" : " seconds") : "";
    return hDisplay + mDisplay + sDisplay; 
}