Trang chủ Bài viết Lập trình Frontend Angular 2 căn bản - Bài 12: Attribute Directives
Lập trình Frontend 23/01/2017 7,170 lượt xem

Angular 2 căn bản - Bài 12: Attribute Directives

Angular 2 căn bản - Bài 12: Attribute Directives

Bài này chúng ta sẽ học 2 buit-in directive trong Angular 2 là ngClass và ngStyle nằm trong loại directive thứ 2 là Attribute Directive.

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

@Component({
    selector: 'my-tutorial',
    template: `
    <h2>{{title}}</h2>
    <p [ngClass]="{classOne:cone,classTwo:ctwo}">This ngClass apply style</p>
    <button (click)="toggle()">Toggle</button>
    <p [ngStyle]="{'font-style':style,'font-size':size}">This paragaph will be apply to ngStyle</p>
    `,
    styles: [
        `
        .classOne{
            color:yellow;
        }
        .classTwo{
            background-color:black;
        }
        `
    ]
})
export class TutorialComponent {
    public cone = true;
    public ctwo = true;
    public style="italic";
    public size="30px";
    toggle(){
        this.cone = !this.cone;
        this.ctwo = !this.ctwo;
    }
}

Chia sẻ:

Bài viết liên quan

Giới thiệu về Angular
08/12/2019 • TEDU

Giới thiệu về Angular

Chào mừng bạn đến với series bài viết hướng dẫn Angular. Series này bao gồm từ Angular 2, 3, 4, 5, 6, 7, 8 và 9. Bao gồm các bước để bắt đầu làm chủ Angular

Đọc thêm
Học ReactJS qua ví dụ
02/06/2019 • Cao Anh Quốc

Học ReactJS qua ví dụ

Tuy chỉ là một thư viện nhưng khi kết hợp hệ sinh thái của React lại ta hoàn toàn có thể gọi ReactJS là một framework và ReactJS là một trong những library/ framework phổ biến nhất hiện nay.

Đọc thêm
Zalo