33

I have a component which defines an imageUrl property and in my template I use this property to set the url of an image. I tried this using interpolation and using property binding, both work, but I cannot find any differences between the two, or when to use the one over the other. Does anyone know the difference?

<img [src]='imageUrl' >

<img src= {{ imageUrl }} >
Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
koninos
  • 4,969
  • 5
  • 28
  • 47
  • 2
    with property binding you set the attribute with the exact value of the property, now with interpolation, you can mix dynamic content from your model within the string, hence the interpolation (ex: `src="http://{{url}}"`) –  May 20 '16 at 14:07
  • Yes like Andre mentioned property binding does not allow you to template/concatenate your existing string it must be exact. But with interpolation you can template around what you have. – inoabrian May 20 '16 at 14:20

5 Answers5

43

Angular evaluates all expressions in double curly braces, converts the expression results to strings, and concatenates them with neighboring literal strings. Finally, it assigns this composite interpolated result to an element or directive/component property. -- from https://angular.io/docs/ts/latest/guide/template-syntax.html#!#interpolation

Property binding does not convert the expression result to a string.

So if you need to bind something other than a string to your directive/component property, you must use property binding.

Mark Rajcok
  • 362,217
  • 114
  • 495
  • 492
16

Interpolation uses the {{ expression }} to render the bound value to the component’s template. Interpolation is a special syntax that Angular converts into a property binding

Property binding uses [] to send values from the component to the template. Property Binding: to set an element property to a non-string data value, you must use property binding

Example :

we are disabling a button by binding to the Boolean property isDisabled.

<button [disabled]='isDisabled'>Try Me</button>

interpolation instead of property binding, the button will always be disabled irrespective of isDisabled class property value is true of false.

<button disabled='{{isDisabled}}'>Try Me</button>

canonical form which is an alternate syntax to square bracket.

 <button bind-disabled='isDisabled'>Try Me</button>
Saksham
  • 9,037
  • 7
  • 45
  • 73
Raja Rama Mohan Thavalam
  • 8,131
  • 2
  • 31
  • 30
1

Interpolation in Angular is just an alternative approach for property binding. It is a special type of syntax that converts into a property binding.

But there are some scenarios where we need to use interpolation instead of property binding. For example, if you want to concatenate strings then you need to use angular interpolation instead of property binding.

import { Component } from '@angular/core';
@Component({
  selector: 'app-root',
    template: `<div>
                    <img src="https://angular.io/assets/images/logos/angular/{{imageName}}">            
               </div>`
})
export class AppComponent {
  imageName: string = "logo-nav2x.png";
}
1

String interpolation and Property Binding

String Interpolation is a special syntax which is converted to property binding by Angular. It's a convenient alternative to property binding.

When you need to concatenate strings, you must use interpolation instead of property binding.

@Component({  
        selector: 'my-app',  
        template: `<div>  
                        <h1>{{interposlationExample}}</h1>  
                    </div>`  
    })  
    export class AppComponent {  
        interposlationExample: string = 'Interpolation for string only';  
      
    }  

Property Binding is used when you have to set an element property to a non-string data value.

Example:

In the following example, we disable a button by binding to the Boolean property isDisabled.

  import { Component } from '@angular/core';  
    @Component({  
        selector: 'my-app',  
        template: `<div>  
        <button [disabled]='isDisabled'>Disable me</button>  
                         </div>`  
    })  
    export class AppComponent {  
    isDisabled: boolean = true;  
    }

If you use interpolation instead of property binding, the button will always be disabled regardless isDisabled class property value is true or false.

import { Component } from '@angular/core';  
@Component({  
    selector: 'my-app',  
    template: `<div>  
    <button disabled='{{isDisabled}}'>Disable Me</button>  
                     </div>`  
})  
export class AppComponent {  
isDisabled: boolean = true/false;  
}
Zubair Saif
  • 1,106
  • 1
  • 14
  • 29
0

From a security standpoint, Angular data binding sanitizes malicious content before displaying it on the browser. Both interpolation and property binding protect us from malicious content.

In the example below we are using interpolation. Notice the malicious usage of tag.

import { Component } from '@angular/core';

@Component({
    selector: 'my-app',
    template: '<div>{{badHtml}}</div>'
})
export class AppComponent {
    badHtml: string = 'Hello <script>alert("Hacked");</script> World';
}

Angular interpolation sanitizes the malicious content and displays the following in the browser

Hello alert("Hacked"); World

In the example below we are using property binding.

'<div [innerHtml]="badHtml">'

Property binding sanitizes the malicious content slightly differently and we get the following output, but the important point to keep in mind is both techniques protect us from malicious content and render the content harmlessly.

Hello alert("Hacked"); World

Reference: https://csharp-video-tutorials.blogspot.com/2017/06/property-binding-in-angular-2.html

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197