Angular MDBootstrap开关组件
MDBootstrap是一个基于Material Design和bootstrap的Angular UI库,通过其无缝和易于使用的组件来制作有吸引力的网页。 在这篇文章中,我们将看到如何在Angular MDBootstrap中使用开关组件。
开关组件用于指示组件的开/关状态。可以利用开关来执行开/关功能,或者提供选择两个预定义选项之一的偏好。
语法:
<div class="custom-control custom-switch">
<input type="checkbox" class="custom-control-input">
<label class="custom-control-label">Switch</label>
</div>
步骤:
- 从官方网站下载Angular MDBootstrap。
- 将文件提取到当前工作目录。
- 使用以下命令在当前项目中安装npm。
npm install
- 在创建你的项目文件夹即appname后,使用以下命令移动到它。
cd appname
- 使用以下命令启动服务器。
ng serve
项目结构:完成安装后,它将看起来像下面这样。
Project Structure
例子1:这是一个基本的例子,说明了如何在Angular MDBootstrap中使用开关组件。在这里,我们实现了默认和检查的开关。
<div id="gfg">
<h1>GeeksforGeeks</h1>
<h4>
Angular MDBootstrap Switch Component
</h4>
<br />
<div class="custom-control custom-switch">
<input type="checkbox"
class="custom-control-input"
id="customSwitches">
<label class="custom-control-label"
for="customSwitches">
Switch
</label>
</div>
<br/>
<div class="custom-control custom-switch">
<input type="checkbox"
class="custom-control-input"
id="gfg1"
checked>
<label class="custom-control-label"
for="gfg1">
Switch
</label>
</div>
</div>
import { AfterViewInit, Component, ViewChildren } from "@angular/core";
@Component({
selector: "app-root",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"],
})
export class AppComponent implements AfterViewInit {
@ViewChildren(AppComponent) collapses!: AppComponent[];
ngAfterViewInit(): void {}
togglevisible = false;
togglebg() {
this.togglevisible = !this.togglevisible;
}
}
import { BrowserModule } from "@angular/platform-browser";
import { NgModule } from "@angular/core";
import { BrowserAnimationsModule }
from "@angular/platform-browser/animations";
import { AppComponent } from "./app.component";
import { MDBBootstrapModule } from "angular-bootstrap-md";
import { FormsModule } from "@angular/forms";
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
BrowserAnimationsModule,
MDBBootstrapModule.forRoot(),
FormsModule,
],
providers: [],
bootstrap: [AppComponent],
})
export class AppModule {}
输出:
例子2:在这个例子中,我们将知道如何在Angular MDBootstrap中禁用开关组件。
<div id="gfg">
<h1>GeeksforGeeks</h1>
<h4>
Angular MDBootstrap Switch Component
</h4>
<br />
<div class="custom-control custom-switch">
<input type="checkbox"
class="custom-control-input"
id="gfg1" disabled>
<label class="custom-control-label"
for="gfg1">
Switch
</label>
</div>
</div>
import { AfterViewInit, Component, ViewChildren } from "@angular/core";
@Component({
selector: "app-root",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"],
})
export class AppComponent implements AfterViewInit {
@ViewChildren(AppComponent) collapses!: AppComponent[];
ngAfterViewInit(): void {}
togglevisible = false;
togglebg() {
this.togglevisible = !this.togglevisible;
}
}
import { BrowserModule } from "@angular/platform-browser";
import { NgModule } from "@angular/core";
import { BrowserAnimationsModule }
from "@angular/platform-browser/animations";
import { AppComponent } from "./app.component";
import { MDBBootstrapModule } from "angular-bootstrap-md";
import { FormsModule } from "@angular/forms";
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
BrowserAnimationsModule,
MDBBootstrapModule.forRoot(),
FormsModule,
],
providers: [],
bootstrap: [AppComponent],
})
export class AppModule {}
输出: