css - 如何在md-Switch上设置文本?

标签 css angularjs material-design angular-ui angular-material

我正在尝试在 Material Angular 设计下的 md-switch 上设置信息文本,以便用户可以知道字段值的当前状态是什么。我想要的图形表示可以是 Text on md-switch

各位,这是怎么做到的?我是 Material 设计新手。

最佳答案

您可以尝试使用 CSS 向 md-bar 添加标签:

md-switch.inline-label .md-bar::before {
  position: absolute;
  font-size: 9px;
  line-height: 13px;

  content: "off";
  right: 0;
  padding-right: 3px;      
}

md-switch.inline-label.md-checked .md-bar::before {
  content: "on";
  left: 0;
  padding-left: 3px;
}

将类inline-label应用到您的md-switch:

<md-switch class="inline-label" ng-model="data.cb1" aria-label="Switch 1">
  Switch 1: {{ data.cb1 }}
</md-switch>

angular.module('MyApp', ['ngMaterial'])
  .controller('SwitchDemoCtrl', function($scope) {
    $scope.data = {
      cb1: true,
      cb4: true,
      cb5: false
    };

    $scope.message = 'false';

    $scope.onChange = function(cbState) {
      $scope.message = cbState;
    };
  });
.switchdemoBasicUsage .inset {
  padding-left: 25px;
  padding-top: 25px;
}
md-switch.wlabel .md-bar::before {
  content: "off";
  position: absolute;
  right: 0;
  font-size: 9px;
  line-height: 13px;
  padding-right: 3px;
}
md-switch.wlabel.md-checked .md-bar::before {
  content: "on";
  left: 0;
  padding-left: 3px;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-animate.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-aria.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angular_material/1.0.3/angular-material.min.js"></script>
<link href="https://ajax.googleapis.com/ajax/libs/angular_material/1.0.3/angular-material.min.css" rel="stylesheet" />

<div class="inset switchdemoBasicUsage" ng-controller="SwitchDemoCtrl" ng-cloak="" ng-app="MyApp">
  <md-switch class="wlabel" ng-model="data.cb1" aria-label="Switch 1">
    Switch 1: {{ data.cb1 }}
  </md-switch>

  <md-switch ng-model="data.cb2" aria-label="Switch 2" ng-true-value="'yup'" ng-false-value="'nope'" class="md-warn">
    Switch 2 (md-warn): {{ data.cb2 }}
  </md-switch>

  <md-switch ng-disabled="true" aria-label="Disabled switch" ng-model="disabledModel">
    Switch (Disabled)
  </md-switch>

  <md-switch ng-disabled="true" aria-label="Disabled active switch" ng-model="data.cb4">
    Switch (Disabled, Active)
  </md-switch>

  <md-switch class="md-primary" md-no-ink="" aria-label="Switch No Ink" ng-model="data.cb5">
    Switch (md-primary): No Ink
  </md-switch>

  <md-switch ng-model="data.cb6" aria-label="Switch 6" ng-change="onChange(data.cb6)">
    Switch 6 message: {{ message }}
  </md-switch>
</div>

关于css - 如何在md-Switch上设置文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35651115/

相关文章:

html - 高度可变的 div 内垂直对齐多个跨度(ui-select-multiple)

html - 显示内部 div 溢出 :visible of parent overflow:hidden

javascript - 在 AngularJS 中更改不同状态的动画

angularjs - 如何使用指数退避实现 angular.js 重试?

javascript - 使用固定列垂直滚动

html - 图像悬停不起作用

javascript - angularJs如何将href添加到$first

css - 如何在窗口调整大小时更改 Bootstrap 网格?

android - Ripple、colorPrimary 或 colorAccent 的颜色应该是什么? ( Material 设计)

android - 如何使用设计库选项卡布局更改工具栏标题?