HI WELCOME TO SIRIS

Style binding in angular 2

Leave a Comment

In this video we will discuss Style binding in Angular with examples.


Setting inline styles with style binding is very similar to setting CSS classes with class binding.

Notice in the example below, we have set the font color of the button using the style attribute.

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

@Component({
    selector: 'my-app',
    template: `
                <button style="color:red">My Button</button>
              `
})
export class AppComponent {
}



The following example sets a single style (font-weight). If the property 'isBold' is true, then font-weight style is set to bold else normal.

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

@Component({
    selector: 'my-app',
    template: `
                <button style='color:red'
                        [style.font-weight]="isBold ? 'bold' : 'normal'">My Button
                </button>
              `
})
export class AppComponent {
    isBold: boolean = true;
}

style property name can be written in either dash-case or camelCase. For example, font-weight style can also be written using camel case - fontWeight.

Some styles like font-size have a unit extension. To set font-size in pixels use the following syntax. This example sets font-size to 30 pixels.

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

@Component({
    selector: 'my-app',
    template: `
                <button style='color:red'
                        [style.font-size.px]="fontSize">My Button
                </button>
              `
})
export class AppComponent {
    fontSize: number = 30;
}

To set multiple inline styles use NgStyle directive as shown below
  • Notice the color style is added using the style attribute
  • ngStyle is binded to addStyles() method of the AppComponent class
  • addStyles() method returns an object with 2 key/value pairs. The key is a style name, and the value is a value for the respective style property or an expression that returns the style value.
  • let is a new type of variable declaration in JavaScript.
  • let is similar to var in some respects but allows us to avoid some of the common gotchas that we run into when using var. 
  • The differences between let and var are beyond the scope of this video. For our example, var also works fine.
  • As TypeScript is a superset of JavaScript, it supports let
import { Component } from '@angular/core';

@Component({
    selector: 'my-app',
    template: ` <button style='color:red' [ngStyle]="addStyles()">My Button</button>
                    `
})
export class AppComponent {
    isBold: boolean = true;
    fontSize: number = 30;
    isItalic: boolean = true;

    addStyles() {
        let styles = {
            'font-weight'this.isBold ? 'bold' : 'normal',
            'font-style'this.isItalic ? 'italic' : 'normal',
            'font-size.px'this.fontSize
        };

        return styles;
    }
}

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.