Ngx-Bootstrap - 按鈕



ngx-bootstrap 按鈕有兩個特定的指令,使一組按鈕的行為像複選框或單選按鈕或混合模式,其中單選按鈕可以取消選中。

ButtonCheckboxDirective

為任何元素新增複選框功能。

選擇器

  • [btnCheckbox]

輸入

  • btnCheckboxFalse − 布林值,假值,將設定為 ngModel,預設值:false

  • btnCheckboxTrue − 布林值,真值,將設定為 ngModel,預設值:true

ButtonRadioDirective

建立單選按鈕或按鈕組。所選按鈕的值繫結到透過 ngModel 指定的變數。

選擇器

  • [btnRadio]

輸入

  • btnRadio − 字串,單選按鈕值,將設定為 ngModel

  • disabled − 布林值,如果為 true - 單選按鈕被停用

  • uncheckable − 布林值,如果為 true - 單選按鈕可以取消選中

  • value − 字串,單選元件或組的當前值

ButtonRadioGroupDirective

一組單選按鈕。所選按鈕的值繫結到透過 ngModel 指定的變數。

選擇器

  • [btnRadioGroup]

示例

由於我們將使用按鈕,因此我們必須更新在ngx-bootstrap 警報章節中使用的 app.module.ts 以使用ButtonsModule。我們還使用 FormModule 新增對輸入控制元件的支援。

更新 app.module.ts 以使用 AlertModule 和 AlertConfig。

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { AppComponent } from './app.component';
import { TestComponent } from './test/test.component';
import { AccordionModule } from 'ngx-bootstrap/accordion';
import { AlertModule,AlertConfig } from 'ngx-bootstrap/alert';
import { ButtonsModule } from 'ngx-bootstrap/buttons';
import { FormsModule } from '@angular/forms';
@NgModule({
   declarations: [
      AppComponent,
      TestComponent
   ],
   imports: [
      BrowserAnimationsModule,
      BrowserModule,
      AccordionModule,
      AlertModule,
      ButtonsModule,
      FormsModule
   ],
   providers: [AlertConfig],
   bootstrap: [AppComponent]
})
export class AppModule { }

更新 test.component.html 以使用按鈕。

test.component.html

<button type="button" class="btn btn-primary" (click)="clicked()">
   Single Button
</button>
<pre class="card card-block card-header">
   {{clickCounter}}
</pre>
<p>Button as Checkbox</p>
<div class="btn-group">
   <label class="btn btn-primary" [(ngModel)]="checkModel.left"
      btnCheckbox tabindex="0" role="button">Left</label>
   <label class="btn btn-primary" [(ngModel)]="checkModel.right"
      btnCheckbox tabindex="0" role="button">Right</label>
</div>
<pre class="card card-block card-header">
   {{checkModel | json}}
</pre>
<p>Button as RadionButton</p>
<div class="form-inline">
   <div class="btn-group" btnRadioGroup [(ngModel)]="radioModel">
      <label class="btn btn-success" btnRadio="Left">Left</label>
      <label class="btn btn-success" btnRadio="Right">Right</label>
   </div>
</div>
<pre class="card card-block card-header">
   {{radioModel}}
</pre>

更新 test.component.ts 以對應變數和方法。

test.component.ts

import { Component, OnInit } from '@angular/core';
@Component({
   selector: 'app-test',
   templateUrl: './test.component.html',
   styleUrls: ['./test.component.css']
})
export class TestComponent implements OnInit {
   checkModel = { left: false, right: false };
   radioModel = 'Left';
   clickCounter = 0;
   constructor() { }

   ngOnInit(): void {
   }
   clicked(): void {
      this.clickCounter++;
   }
}

構建和啟動

執行以下命令以啟動 Angular 伺服器。

ng serve

伺服器啟動並執行後。開啟 https://:4200 並驗證以下輸出。

Buttons
廣告

© . All rights reserved.