Javascript 如何使用[(ngModel)]双向数据绑定在Angular 4中创建切换开关on-off按钮

Javascript 如何使用[(ngModel)]双向数据绑定在Angular 4中创建切换开关on-off按钮,javascript,css,html,angular,twitter-bootstrap,Javascript,Css,Html,Angular,Twitter Bootstrap,需要实现与所附图像类似的开关按钮…使用库 安装npm安装ngx切换开关--保存 用法:- import { UiSwitchModule } from 'ngx-toggle-switch'; import { AppComponent } from './app.component'; @NgModule({ imports: [BrowserModule, UiSwitchModule], declarations: [AppComponent], bootstrap: [Ap

需要实现与所附图像类似的开关按钮…

使用库

安装
npm安装ngx切换开关--保存

用法:-

import { UiSwitchModule } from 'ngx-toggle-switch';
import { AppComponent } from './app.component';

@NgModule({
  imports: [BrowserModule, UiSwitchModule],
  declarations: [AppComponent],
  bootstrap: [AppComponent]
})
export class AppModule {
}
在标记语言中:-

<ui-switch></ui-switch>

用于双向装订

<ui-switch [(ngModel)]="enable"></ui-switch>


有关Packge的文档,请参阅:-

您可以使用普通复选框和一些css获得功能。 下面是有帮助的代码

Css:

.switch {
  position: relative;
  display: inline-block;
  width: 45px;
  height: 20px;
  margin: 20px;
}

.switch input {display:none; background-color: #ccc;}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
   background-color: #ccc;
  -webkit-transition: .4s;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 0px;
   right: 0px;
  bottom: -3px;
  background-color: #2196F3;
  -webkit-transition: .4s;
  transition: .4s;
}

input:checked + .slider:before {
  -webkit-transform: translateX(20px);
  -ms-transform: translateX(20px);
  transform: translateX(20px);
}

/* Rounded sliders */
.slider.round {
  border-radius: 20px;
}

.slider.round:before {
  border-radius: 50%;
}
Html:

<div class="row">
                <label>Yes</label>
                <label class="switch">
                <input type="checkbox" [(ngModel)]="isNewProfile">
                <span class="slider round"></span>
                </label>
                <label>No</label>
        </div>

对
不

可以帮助您开始。感谢您的解决方案。但是,有没有可能通过启动开关的开/关按钮来让它看起来漂亮呢?css样式将类似于为您的解决方案附加的图像。我不想用你建议的任何第三方插件或库来实现它。是否可以使用CSS或引导切换开关与angular4实现?是否有任何理由不使用第三方插件或库…?没有具体原因,但想尝试使用开源引导或普通CSS实现