Wednesday, 6 March 2019

@Attribute decorator in Angular Js

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

export type ButtonType = 'primary' | 'secondary';

@Component({
    selector: 'app-button',
    template: `
    <button [ngClass]="type">
      <ng-content></ng-content>
    </button>
  `
})
export class ButtonComponent {

    constructor(@Attribute('type') public type: ButtonType = 'primary') {}

}



  @Attribute decorator in Angular 2 
 Angular will evaluate it once and forget about it. As a general rule, I prefer the @Attribute() approach when the string is a fixed value that never changes.

Use Example 

<app-button type="secondary" (click)="click()">Click</app-button>

No comments:

Post a Comment