18 May 2023

Ng-Content Switch Case - Angular

                                    _             _                     _ _       _                           
  _ __   __ _        ___ ___  _ __ | |_ ___ _ __ | |_      _____      _(_) |_ ___| |__      ___ __ _ ___  ___ 
 | '_ \ / _` |_____ / __/ _ \| '_ \| __/ _ \ '_ \| __|    / __\ \ /\ / / | __/ __| '_ \    / __/ _` / __|/ _ \
 | | | | (_| |_____| (_| (_) | | | | ||  __/ | | | |_     \__ \\ V  V /| | || (__| | | |  | (_| (_| \__ \  __/
 |_| |_|\__, |      \___\___/|_| |_|\__\___|_| |_|\__|    |___/ \_/\_/ |_|\__\___|_| |_|   \___\__,_|___/\___|
        |___/
  • Directive NgxGaugeLabel
@Directive({
  selector: "ngx-gauge-label",
  exportAs: "ngxGaugeLabel"
})
export class NgxGaugeLabel {}
  • Component NgxGauge
@Component({
  selector: 'ngx-gauge'
})
export class NgxGauge {
  @ContentChild(NgxGaugeLabel) _labelChild: NgxGaugeLabel;
}
  • Component NgxGauge
<div [ngSwitch]="_labelChild != null">
  <ng-content select="ngx-gauge-label" *ngSwitchCase="true"></ng-content>
  <ng-container *ngSwitchCase="false">{{label}}</ng-container>
</div>
  • NgModule NgxGaugeModule
@NgModule({
  imports: [CommonModule],
  declarations: [
    NgxGauge,// Component
    NgxGaugeLabel // Directive
  ],
  exports: [
    NgxGaugeLabel,
  ],
})
export class NgxGaugeModule {}
  • TEST
<ngx-gauge>
    <ngx-gauge-label>
      <h1>123</h1>
    </ngx-gauge-label>
</ngx-gauge>

-- Angular 2+ --

view raw ng-content.md hosted with ❤ by GitHub

Related Posts:

  • Ng serve --live-reload trong Angular 2 Sử dụng ng serve --live-reload chạy lệnh ng serve --live-reload trên terminal Hoặc thêm lệnh ng serve --live-reload vào file package.json và chạy npm start trên terminal … Read More
  • HostListener event scroll Angular 2+ Event scroll Angular 2+ Angular 2+ import { Component, HostListener, Inject, NgModule, OnInit } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { D… Read More
  • ControlValueAccessor TwoWay Binding trong Angular 2+  ControlValueAccessor TwoWay Binding interface ControlValueAccessor { writeValue(obj: any): void; registerOnChange(fn: any): void; registerOnTouched(fn: any): void; setDisabledState?(isDisabled: boolean… Read More
  • HostListener event key Angular 2+ Event key Angular 2+ Angular 2+ @HostListener('window:keydown', ['$event']) keyboardInput(event: any) { event.preventDefault(); event.stopPropagation(); this.which = event.which; this.keyCode = … Read More
  • Validation formBuilder trong Angular 2+ Validation FormBuilder Bước 1: Tạo project Angular 2 bấm vào đây app.component.ts Angular 2+ import { FormGroup, FormBuilder, Validators } from '@angular/forms'; constructor(private formBuilder… Read More

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang