如何在 angularjs 材料滑块中显示文本代替数字
How to display text in place of number in angularjs materials silder
当您单击滑块时,我想要什么,然后在其工具提示上显示文本代替数字。
代码如下:
HTML:
<div ng-controller="AppCtrl" class="sliderdemoBasicUsage" ng-app="MyApp">
<br>
<br>
<br>
<br>
<md-content style="margin: 16px; padding:16px">
<br>
<br>
<md-slider flex class="md-primary" md-discrete ng-model="rating3" step="1" min="1" max="5" aria-label="rating">
</md-slider>
</md-content>
</div>
Angular代码:
angular.module('MyApp')
.controller('AppCtrl', function($scope) {
});
或者您可以参考以下link https://codepen.io/ankur-tiwari/pen/bVNEKJ
遗憾的是,该功能不受支持,因为它在技术上不是 material spec for sliders 的一部分。
对于李克特量表,您应该考虑 selection controls spec of material. Otherwise, you can get away with it in Angular Material by using a horizontal row of radio buttons。
<p ng-bind="val" ng-init="val=20"></p>
<div class="slider" ui-jq="slider" ng-slider-model="val" ui-options="sliderOpt4"></div>
</div>
这是一个指令,您可以使用 html 和 css
访问和显示控制器值的值。
.directive('ngSliderModel', ['$parse', function($parse) {
return {
scope: {
ngSliderModel: '=',
uiOptions: '='
},
restrict: 'A',
required: ['ngSliderModel'],
link: function(scope, element, attrs) {
// check there is uiOption or not
var options = ('uiOptions' in attrs) ? scope.uiOptions : {};
// get the value of ngSlider Model
var val = scope.ngSliderModel;
// if value is range [15,25] then return values for uiOptions propertyName else value for singles
var propName = (angular.isArray(val)) ? 'values' : 'value';
/* if you want to slide when the scope value changed not from slider...
watch the ngSliderModel attribute
*/
scope.$watch('ngSliderModel', function(newValue){
element.slider(propName, newValue);
});
// set value for options
options[propName] = val;
// binding slide event
element.bind('slide', function() {
// Read the current value
var value = element.slider('option', propName);
scope.$apply(function() {
// Apply the value to scope
scope.ngSliderModel = value;
});
});
}
};
}]);
Angular Material不支持
有关 angular-material 模块
的 slider.js 的更多详细信息检查
function SliderDirective($$rAF, $window, $mdAria, $mdUtil, $mdConstant, $mdTheming, $mdGesture, $parse, $log) {
return {
scope: {},
require: '?ngModel',
template:
'<div class="md-slider-wrapper">\
<div class="md-track-container">\
<div class="md-track"></div>\
<div class="md-track md-track-fill"></div>\
<div class="md-track-ticks"></div>\
</div>\
<div class="md-thumb-container">\
<div class="md-thumb"></div>\
<div class="md-focus-thumb"></div>\
<div class="md-focus-ring"></div>\
<div class="md-sign">\
<span class="md-thumb-text"></span>\
</div>\
<div class="md-disabled-thumb"></div>\
</div>\
</div>',
compile: compile
};
在此代码中
<span class="md-thumb-text"></span>
用于渲染滑块的范围数。
并使用以下方法来呈现它
function postLink(scope, element, attr, ngModelCtrl){
}
你可以这样修改函数的内容
var _values = ["very low", "low", "average", "good", "best"];
thumbText.text( _values[ngModelCtrl.$viewValue] );
呈现文本而不是整数值。
Angular-material 目前不支持此功能,您如何通过编写自己的逻辑或创建自己的滑块来完成此操作。
Material 2 团队已经解决了这个问题,可能很快就会提出解决方案。
https://github.com/angular/material2/issues/4776
最终在将 angular.js 更新为 Angular 之后,Angular Material 为 Angular 提供了 自定义缩略图标签格式 的选项滑块。
import {Component} from '@angular/core';
/**
* @title Slider with custom thumb label formatting.
*/
@Component({
selector: 'slider-formatting-example',
templateUrl: 'slider-formatting-example.html',
styleUrls: ['slider-formatting-example.css'],
})
export class SliderFormattingExample {
formatLabel(value: number | null) {
if (!value) {
return 0;
}
if(value < 250)
{
return 'Low';
}
else if(value > 250 && value < 750)
{
return 'Medium';
}
else
{
return 'High';
}
return value;
}
}
Html代码:
<mat-slider
thumbLabel
[displayWith]="formatLabel"
tickInterval="1000"
min="1"
max="100000"></mat-slider>
Note: I update my app from angular.js to Angular latest version.
当您单击滑块时,我想要什么,然后在其工具提示上显示文本代替数字。
代码如下:
HTML:
<div ng-controller="AppCtrl" class="sliderdemoBasicUsage" ng-app="MyApp">
<br>
<br>
<br>
<br>
<md-content style="margin: 16px; padding:16px">
<br>
<br>
<md-slider flex class="md-primary" md-discrete ng-model="rating3" step="1" min="1" max="5" aria-label="rating">
</md-slider>
</md-content>
</div>
Angular代码:
angular.module('MyApp')
.controller('AppCtrl', function($scope) {
});
或者您可以参考以下link https://codepen.io/ankur-tiwari/pen/bVNEKJ
遗憾的是,该功能不受支持,因为它在技术上不是 material spec for sliders 的一部分。
对于李克特量表,您应该考虑 selection controls spec of material. Otherwise, you can get away with it in Angular Material by using a horizontal row of radio buttons。
<p ng-bind="val" ng-init="val=20"></p>
<div class="slider" ui-jq="slider" ng-slider-model="val" ui-options="sliderOpt4"></div>
</div>
这是一个指令,您可以使用 html 和 css
访问和显示控制器值的值。.directive('ngSliderModel', ['$parse', function($parse) {
return {
scope: {
ngSliderModel: '=',
uiOptions: '='
},
restrict: 'A',
required: ['ngSliderModel'],
link: function(scope, element, attrs) {
// check there is uiOption or not
var options = ('uiOptions' in attrs) ? scope.uiOptions : {};
// get the value of ngSlider Model
var val = scope.ngSliderModel;
// if value is range [15,25] then return values for uiOptions propertyName else value for singles
var propName = (angular.isArray(val)) ? 'values' : 'value';
/* if you want to slide when the scope value changed not from slider...
watch the ngSliderModel attribute
*/
scope.$watch('ngSliderModel', function(newValue){
element.slider(propName, newValue);
});
// set value for options
options[propName] = val;
// binding slide event
element.bind('slide', function() {
// Read the current value
var value = element.slider('option', propName);
scope.$apply(function() {
// Apply the value to scope
scope.ngSliderModel = value;
});
});
}
};
}]);
Angular Material不支持
有关 angular-material 模块
的 slider.js 的更多详细信息检查function SliderDirective($$rAF, $window, $mdAria, $mdUtil, $mdConstant, $mdTheming, $mdGesture, $parse, $log) {
return {
scope: {},
require: '?ngModel',
template:
'<div class="md-slider-wrapper">\
<div class="md-track-container">\
<div class="md-track"></div>\
<div class="md-track md-track-fill"></div>\
<div class="md-track-ticks"></div>\
</div>\
<div class="md-thumb-container">\
<div class="md-thumb"></div>\
<div class="md-focus-thumb"></div>\
<div class="md-focus-ring"></div>\
<div class="md-sign">\
<span class="md-thumb-text"></span>\
</div>\
<div class="md-disabled-thumb"></div>\
</div>\
</div>',
compile: compile
};
在此代码中
<span class="md-thumb-text"></span>
用于渲染滑块的范围数。 并使用以下方法来呈现它
function postLink(scope, element, attr, ngModelCtrl){
}
你可以这样修改函数的内容
var _values = ["very low", "low", "average", "good", "best"];
thumbText.text( _values[ngModelCtrl.$viewValue] );
呈现文本而不是整数值。
Angular-material 目前不支持此功能,您如何通过编写自己的逻辑或创建自己的滑块来完成此操作。
Material 2 团队已经解决了这个问题,可能很快就会提出解决方案。 https://github.com/angular/material2/issues/4776
最终在将 angular.js 更新为 Angular 之后,Angular Material 为 Angular 提供了 自定义缩略图标签格式 的选项滑块。
import {Component} from '@angular/core';
/**
* @title Slider with custom thumb label formatting.
*/
@Component({
selector: 'slider-formatting-example',
templateUrl: 'slider-formatting-example.html',
styleUrls: ['slider-formatting-example.css'],
})
export class SliderFormattingExample {
formatLabel(value: number | null) {
if (!value) {
return 0;
}
if(value < 250)
{
return 'Low';
}
else if(value > 250 && value < 750)
{
return 'Medium';
}
else
{
return 'High';
}
return value;
}
}
Html代码:
<mat-slider
thumbLabel
[displayWith]="formatLabel"
tickInterval="1000"
min="1"
max="100000"></mat-slider>
Note: I update my app from angular.js to Angular latest version.