String Interpolation {{}} in Angular 4 – Must to know Things

String Interpolation {{}} in Angular 4 – Must to know Things:

String interpolation is basically used to output the values from the typescript file to template file. Whenever you want to dynamically display the values through backends/http requests/from DB then you can make use of this string interpolation to directly output your contents to templates.

But know the things mentioned below to understand and use it at the appropriate places.

 

  1. String interpolation is defined in angular with {{}} [Double curly braces].
    2. Any type of variable can be used inside the {{}}. It is not limited to use only string as the name is string interpolation. [;)].
    3. Not allowed to write if/for/block of statements inside {{}} [ String interpolation].
    4. Methods can also be called inside the {{}} [ String interpolation ].

 

 

Code Examples:

export class MyComponent {
studentID = 100;
studentName = 'Rithvik';
}

 

By default typescript understands the number and string, but if you like to explicitly mention then the same can be rewritten this way:


export class MyComponent {
studentID:number = 100;
studentName: string = 'Rithvik';
studentPerc: string = '98%';

getStudentPerc(){
return this.studentPerc;
}
}

here string is full small letter, java developers may write String instead of string.

in html file:


Student ID: {{studentID}} | Student Name : {{studentName}} | Student Perc : {{getStudentPerc()}}

 

Output:

Student ID: 100 | Student Name : Rithvik | Student Perc : 98%

 

 

365 total views, 3 views today

Leave a Reply

Your email address will not be published. Required fields are marked *