Angular Material 7 - 滑塊



<mat-slider>是一個Angular指令,用作具有材質設計樣式和動畫功能的增強型範圍選擇器。

在本章中,我們將展示使用Angular Material繪製滑塊控制元件所需的配置。

建立Angular應用程式

按照以下步驟更新我們在Angular 6 - 專案設定章節中建立的Angular應用程式:

步驟 描述
1 按照Angular 6 - 專案設定章節中的說明,建立一個名為materialApp的專案。
2 修改app.module.tsapp.component.tsapp.component.cssapp.component.html,如下所述。其餘檔案保持不變。
3 編譯並執行應用程式以驗證已實現邏輯的結果。

以下是修改後的模組描述符app.module.ts的內容。

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {MatSliderModule, MatCheckboxModule} from '@angular/material'
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
@NgModule({
   declarations: [
      AppComponent
   ],
   imports: [
      BrowserModule,
      BrowserAnimationsModule,
      MatSliderModule, MatCheckboxModule,
      FormsModule,
      ReactiveFormsModule
   ],
   providers: [],
   bootstrap: [AppComponent]
})
export class AppModule { }

以下是修改後的HTML主機檔案app.component.html的內容。

<mat-slider
   class = "tp-margin"
   [disabled] = "disabled"
   [invert] = "invert"      
   [thumbLabel] = "thumbLabel"     
   [(ngModel)] = "value"
   [vertical] = "vertical">
</mat-slider>
<section class = "tp-section">
   <mat-checkbox class = "tp-margin" [(ngModel)] = "thumbLabel">Show thumb label</mat-checkbox>
</section>
<section class = "tp-section">
   <mat-checkbox class = "tp-margin" [(ngModel)] = "vertical">Vertical</mat-checkbox>
   <mat-checkbox class = "tp-margin" [(ngModel)] = "invert">Inverted</mat-checkbox>
</section>
<section class = "tp-section">
   <mat-checkbox class = "tp-margin" [(ngModel)] = "disabled">Disabled</mat-checkbox>
</section>

以下是修改後的CSS檔案app.component.css的內容。

.tp-section {
   display: flex;
   align-content: center;
   align-items: center;
   height: 60px;
}
.tp-margin {
   margin: 30px;
}

.mat-slider-horizontal {
   width: 300px;
}
.mat-slider-vertical {
   height: 300px;
}

以下是修改後的ts檔案app.component.ts的內容。

import { Component } from '@angular/core';
@Component({
   selector: 'app-root',
   templateUrl: './app.component.html',
   styleUrls: ['./app.component.css']
})
export class AppComponent {
   title = 'materialApp'; 
   disabled = false;
   invert = false;
   thumbLabel = false;
   value = 0;
   vertical = false;
}

結果

驗證結果。

Slider

詳情

  • 首先,我們使用mat-checkbox建立了四個複選框,並使用ngModel將它們與變數繫結。這些屬性將用於自定義滑塊。

  • 然後,我們建立了滑塊,並展示了其與.ts檔案中的變數繫結的各種屬性。

廣告
© . All rights reserved.