• Home
  • Categories
  • Video Tutorials
    • Angular 5
  • News
  • About us
  • Contact us
  • Login
test
Code4Developers

Code4Developers

Code4Developers
  • Home
  • Categories
  • Video Tutorials
    • Angular 5
  • News
  • About us
  • Contact us
  • Login
  • Angular

Interpolation & Property Binding in Angular 4

  • Nisarg Dave
  • February 22, 2018
  • 2 minute read
Angular
Total
0
Shares
0
0
0

In this article, we will discuss about interpolation and property binding in Angular 4. Both are used to bind component class properties to view template.

Examples

Let’s take simple example of both

  • We will bind imagePath property of the component class to <img>element src property using interpolation as shown below.
    <img src='{{imagePath}}’/>
  • We can achieve the same using Property binding also. Check bellow example of property biding. <img [src]=’imagePath’/>
  • Here notice the <img>element src property is in a pair of square brackets, and the component class property is in quotes.

Interpolation and Property binding will drive value in one direction, from a component’s data property into target element property.

Difference between Interpolation and Property binding

  • Where to use Interpolation?
    • In some cases like when we need to concatenate strings we have to use interpolation instead of property binding.
    • Check this example.
      <img src=’code4developers.com/{{imagePath}}’ />
  • Where to use Property binding?
    • When setting an element property to a non-string data value, you must use property binding.
    • In the following example, we are disabling a button by binding to the boolean property isDisabled.
      <button [disabled]=’isDisabled’>Click me</button>
    • If we use interpolation instead of property binding, the button is always disabled irrespective of isDisabled class property value.
      <button disabled='{{isDisabled}}’>Click me</button> (this doesn’t work).

 Points to keep in mind when using Property binding

  • Remember to enclose the property name with a pair of square brackets. If you omit the brackets, Angular treats the string as a constant and initializes the target property with that string.
    <span [innerHTML]=’pageHeader’></span>
  • With Property binding we enclose the element property name in square brackets
    <button [disabled]=’isDisabled’>Click me</button>
  • We can also use the alternate syntax with bind- prefix. This is known as canonical form
    <button bind-disabled=’isDisabled’>Click me</button>

 Provide security from malicious content 

  • From security standpoint, Angular data binding sanitizes malicious content before displaying it on the browser.
  • Both interpolation and property binding protects us from malicious content.
  • Check bellow example, we are using interpolation. Notice the malicious usage of <script> tag.
import { Component } from '@angular/core';

@Component({
    selector: 'my-app',
    template: '<div>{{badHtml}}</div>'
})

export class AppComponent {
    badHtml: string = 'Hello <script>alert("Hacking");</script> World';
}
  • Angular interpolation sanitizes the malicious content and displays the following in the browser
    Hello <script>alert(“Hacking “);</script> World
  • Let’s change the code and use property binding.
    ‘<div [innerHtml]=”badHtml”></div>’
  • Property binding sanitizes the malicious content slightly differently and we get the following output : Hello alert(“Hacking”); World

So here the important point to keep in mind is both the techniques protect us from malicious content and render the content harmlessly.

 

Click Here to learn more on Angular from our video tutorials.

Nisarg Dave
Nisarg Dave

He works as a software developer. He has hands-on experience on .net and other Microsoft technologies. He also works on AngularJS, KnockOutJS.

Views: 8,183

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on WhatsApp (Opens in new window) WhatsApp
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on X (Opens in new window) X
  • Click to share on Pinterest (Opens in new window) Pinterest
  • Click to email a link to a friend (Opens in new window) Email
  • Click to print (Opens in new window) Print

Like this:

Like Loading...

Related Posts

Total
0
Shares
Share 0
Tweet 0
Pin it 0

Leave a ReplyCancel reply

Subscribe to Website via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Recent Posts
  • React Hooks Guide: Top Tips for Optimizing Performance in Your React Applications

    React Hooks Guide: Top Tips for Optimizing Performance in Your React Applications

    1 year ago
  • Demystifying JavaScript Tree Shaking: Boosting Performance and Reducing Bundle Size

    Demystifying JavaScript Tree Shaking: Boosting Performance and Reducing Bundle Size

    2 years ago
  • Unlocking the Power of React Hooks: A Comprehensive Guide with Examples

    Unlocking the Power of React Hooks: A Comprehensive Guide with Examples

    2 years ago
  • Celebrating a Decade of Phenomenal Growth: Insights and Reflections on 10 Years of Software Engineering

    Celebrating a Decade of Phenomenal Growth: Insights and Reflections on 10 Years of Software Engineering

    2 years ago
  • Angular Custom Elements: Creating Reusable Components with Angular

    Angular Custom Elements: Creating Reusable Components with Angular

    2 years ago
  • Connect Firebase Realtime NoSQL Database with Angular App from Scratch

    Connect Firebase Realtime NoSQL Database with Angular App from Scratch

    5 years ago
  • How to Build an Inclusive Esports Community

    How to Build an Inclusive Esports Community

    5 years ago
  • Best Digital Icebreakers

    Best Digital Icebreakers

    5 years ago
  • Email alerts when a docker container stopped in AWS ECS CLUSTER

    Email alerts when a docker container stopped in AWS ECS CLUSTER

    5 years ago
  • New Learning Models for Fall 2020

    New Learning Models for Fall 2020

    5 years ago
Subscribe to Website via Email

Enter your email address to subscribe to this website and receive notifications of new posts by email.

Featured Posts
  • javascript 1
    Spread syntax (three dots) in JavaScript
    • March 21, 2018
  • Angular 2
    Angular 6 CRUD – Part 1: Project Setup, Routing, Service
    • May 9, 2018
  • javascript 3
    Local Storage and Session Storage
    • May 22, 2017
  • Angular 4
    Angular 4 Project Structure
    • June 18, 2017
  • AWS 5
    Email alerts when a docker container stopped in AWS ECS CLUSTER
    • July 24, 2020
Code4Developers
Learning is never ending process

Input your search keywords and press Enter.

%d