Wednesday, 19 February 2020

Angular 8 Session 11 - Class Binding In Angular

Class Binding In Angular

* Class Binding is another form of data-binding, used to render classes dynamically.
* There are specifically 2 types of class binding we have in Angular.
Single Class Binding        Multiple Class Binding 
* Normally we used to have our class applied to elements without using any binding as below:
<input type=“text” value=“Welcome to SKMDotNet Angular” class=“bgColor”> 
* To add/remove classes to/from an element we are using Single Class Binding of angular which start with prefix class followed by a dot (.) and name of class, enclosed in a pair of square braces and with an Boolean expression. i.e. [class.bgColor]=“isBgColorRequired”.


<input type=“text” [value]='textValue' class='bgColor font shapeStyle' [class.bgColor]='isBgColorRequired'> 
* To have Multiple Class Binding we need to make use of generic syntax i.e. [class]=‘expression’ where the expression is a pair of string values. 


<input type=“text” [value]='textValue' [class]='classesToApply’>  where classesToApply = "bgColor font shapeStyle"; 
* A multiple class binding can be also bind with help of a method expression.
<input type=“text” [value]='textValue' [className]='getClasses()'>
 where
getClasses() {
    return 'bgColor font shapeStyle';
  };
* The NgClass directive can be used as an alternative to direct [class] bindings. However, using the above class binding syntax without NgClass is preferred because due to improvements in class binding in Angular, NgClass no longer provides significant value, and might eventually be removed in the future.

<input type=“text” value=“Welcome to SKMDotNet Angular” class=“bgColor”>
app.component.html
<div style="text-align: center;">
  <h1>welcome to {{myData}} application</h1>
  <br /><br /><br />
  <h5>Appearance from simple class/style we do have</h5>
  <input type="text" value="Welcome to SKMDotNet Angular" class="bgColor" />
  <br /><br />
  <h5>Appearance from single class binding</h5>
  <input type=“text” [value]='textValue' class='bgColor font shapeStyle' [class.bgColor]='isBgColorRequired'>
  <br /><br />
  <h5>Appearance from multiple class binding out of an expression</h5>
  <input type=“text” [value]='textValue' [class]='classesToApply'>
  <br /><br />
  <h5>Appearance from multiple class binding using a method expression</h5>
  <input type=“text” [value]='textValue' [className]='getClasses()'>
  <br /><br />
  <h5>Appearance from multiple class binding using ngClass</h5>
  <input type=“text” [value]='textValue' [ngClass]='getClasses()'>
</div>

app.component.ts
import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent {
  myData = "angular";
  textValue = "Welcome to SKMDotNet Angular";
  isBgColorRequired = true;
  classesToApply = 'bgColor font shapeStyle';

  getClasses() {
    return 'bgColor font shapeStyle';
  };
};


app.component.css
.bgColor{
    background-colormagenta;
}

.font{
    font-familyArialHelveticasans-serif;
    font-sizemedium;
}

.shapeStyle{
height50px;
width500px;
}

Monday, 10 February 2020

Angular 8 Session 10 - Attribute Binding In Angular

Attribute Binding


* Attribute Binding is another form of one-way data-binding in angular like interpolation and property binding we have which deals with html element attributes.
HTML Attributes vs DOM Properties
* Attributes are defined by HTML elements where properties get converted by DOM from those attributes.
* Angular always deals with element properties and events, not with attributes. Attribute is responsible for initialises DOM properties.
* As per our last example we saw we have been using the disabled attribute for data binding, but its logically not true as internally we are assigning the value to its properties.
<button [disabled]=‘isEnabled’>Click Me</button>

* Attributes value can’t be changed but DOM properties value can be changed. 
* To check in browser developer tool's console window make use of element's   id + . + value to get properties value to get attribute value element's id + . + getAttribute("value").

Attribute Binding
* There is one exception to rule that binding happens with element properties but in case    of attribute binding it only deals with element attributes.
* Usually property binding preferred to set an element’s property but in some cases there are some attributes doesn’t has the respective properties to bind like SVG, colspan, aria etc…
* It’s syntax is similar to property binding but instead of element property it resides with attr prefix followed by a dot (.) to name of element attribute.
<button [attr.aria-label]="actionName">{{actionName}} with Aria</button>

* Similarly, consider to set a colspan attribute using following approach.
<button [attr.aria-label]="actionName">{{actionName}} with Aria</button>

* There is one major difference between colspan & colSpan is colSpan is a property and can be binded with property binding as discussed in previous tutorial but colspan not able to perform same.
app.component.cs

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

@Component({
  selector: 'my-app',
  templateUrl: './app.component2.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent {
  title = 'Angular8Demo';
  myData = "angular";
  isEnabled = false;
  colspan = 2;
}

app.component2.html

<div style="text-align: center;">
    <button [disabled]='isEnabled'>Click me</button>
    <input type="text" id="txtDemo" value="SKMDotNet" />
    <br /><br />
    <button [attr.aria-label]="myData">{{title}}</button>
    <br /><br />
    <table border="2">
        <thead>
            <tr>
                <td>First Name</td>
                <td>Last Name</td>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td [attr.colspan]="colspan">Sunil Kumar Malana</td>
            </tr>
        </tbody>
    </table>
</div>


Angular 8 Session 21 - ngFor Directive In Angular

ngFor Directive Content will be resume soon. Stay tuned!