简体   繁体   English

angular-bootstrap-slider默认值/双向绑定

[英]angular-bootstrap-slider default value / two-way binding

Using angular-bootstrap-slider based on bootstrap-slider . 使用基于bootstrap-slider的 angular- bootstrap-slider This extends a previous question regarding get value as string 这扩展了以前关于将值作为字符串的问题

I want to set the shown value when the page is loaded to be any other value than the first one. 我想在页面加载时将显示的值设置为除第一个之外的任何其他值。

Let's say I want 'blue' to be shown and not 'red' 假设我想要'蓝色'显示而不是'红色'

How do I set a value that will be returned from another $scope. 如何设置将从另一个$ scope返回的值。

Or perhaps how do I set the default value of the slider? 或者如何设置滑块的默认值?

I have a working Plunker example and code below 我有一个有效的Plunker示例和代码如下

script: 脚本:

  $scope.colors = [{'id': 0,'label': 'red'},{'id': 1,'label': 'blue'},{'id': 2,'label': 'green'}];

  function getAttrs(collection, attr) {
    var attrArray = [];
    angular.forEach(collection, function(item) {
      attrArray.push(item[attr]);
    }) 
    return attrArray;
  }

$scope.colorTypeSlider = {
    ticks: getAttrs($scope.colors, 'id'), // [0,1,3]
    ticks_labels: getAttrs($scope.colors, 'label'), // ['red', 'blue','green']
    ticks_snap_bounds: 50,
};

$scope.selectedColor = '';

$scope.selectColor = function ($event,value,collection) {
  console.log('value is ' + value);
  $scope.selectedColor = collection[value];
};

html: HTML:

 <slider ng-model="colors" 
        ticks="colorTypeSlider.ticks"
        ticks-labels="colorTypeSlider.ticks_labels"
        ticks-snap-bounds="colorTypeSlider.ticks_snap_bounds" 
        on-start-slide="selectColor($event,value,colorTypeSlider.ticks_labels)"></slider>
                <h6> value check is: {{selectedColor}} </h6>

I had a dig into the code and noticed that the Slider for Bootstrap (bootstrap-slider.js) uses the attribute "value" to set its initial values. 我深入研究了代码并注意到Slider for Bootstrap(bootstrap-slider.js)使用属性“value”来设置其初始值。 See here for examples . 请看这里的例子 Then in the angular-bootstrap-slider (slider.js) which this sits on, passes in two attributes "ngModel" and "value". 然后在它所在的angular-bootstrap-slider(slider.js)中传入两个属性“ngModel”和“value”。 The initSlider() function then does some decision making and messes around with the ngModel value incorrectly I believe. 然后,initSlider()函数做了一些决策,并且错误地认为ngModel值是错误的。 I found that if I ignore the "ngModel" and just rely on the "value" attribute I could default the initial value of the slider. 我发现如果我忽略“ngModel”并且只依赖于“value”属性,我可以默认滑块的初始值。

<span slider range="true" 
      precision="0" 
      scale="'logarithmic'" 
      ng-model="ngModel" 
      value="ngModel" 
      min="minValue" 
      step="1000" 
      max="maxValue"
      on-stop-slide="setValue()" 
      slider-tooltip="hide">
</span>

So in my case above (from my directive containing the slider) you can see I have set the directives ngModel the both the sliders ng-model (which I don't care about anymore) and also the value (which seems to work). 所以在上面的例子中(从我的包含滑块的指令)你可以看到我已经将指令ngModel设置为滑块ng-model(我不再关心)以及值(看起来有效)。 ng-model is required by angular-bootstrap-slider so I had to provide it but it doesn't seem to hurt setting the value to the same attribute. angular-bootstrap-slider需要ng-model,所以我不得不提供它,但似乎不会将值设置为相同的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM