简体   繁体   中英

How to bind the values dynamically in angular-bootstrap-model?

Here I have to bind the ticks, value, maximum and minimum values dynamically by using angular-bootstrap-slider. But I am unable to bind the values.

CONTROLLER:

var app = angular.module("sliderApp", ['ui.bootstrap-slider']);

app.controller('sliderCtrl', function ($scope, $http) {
    var labels =[{ "val": 1, "txt": "one" }, { "val": 2, "txt": "2" }, { "val": 3, "txt": "3" }, { "val": 4, "txt": "4" }, { "val": 5, "txt": "5+" }];
    var ticks = [12, 24, 36, 48, 60, 72];
    /* maximum value functionality*/
    function getMax(labels, prop) {
        var max;
        for (var i = 0 ; i < labels.length ; i++) {
            if (!max || parseInt(labels[i][prop]) > parseInt(max[prop]))
                max = labels[i];
        }
        return max;
    }
    $scope.sliderMaxVal = getMax(labels, "val").val;
    console.log($scope.sliderMaxVal);

    /* Minimum value functionality*/
    function getMin(labels, prop) {
        var min;
        for (var i = 0 ; i < labels.length ; i++) {
            if (!min || parseInt(labels[i][prop]) < parseInt(min[prop]))
                min = labels[i];
        }
        return min;
    }
    $scope.sliderMinVal = getMin(labels, "val").val;

    $scope.vfg = 1200;

    this.sliderValue = 53;
    this.sliderLabels = function () {
        return labels;
    }
    this.sliderTicks = function () {
        return ticks;
    }
    this.myFormatter = function (value) {
        console.log('value %s', value);
        return '<span class="tooltip-header-title">' + value + ' months </span><br> ' + (value - 1) + ' payments + VFG <span class="glyphicon glyphicon glyphicon-tags" aria-hidden="true"></span>';
    }
});

HTML:

  <div class="col-md-4 .col-xs-4">
            <div ng-controller="sliderCtrl as slider">
                <slider ng-model="slider.sliderValue" min="{{sliderMinValue}}" step="1" max="72" value="sliderApp.sliderValue" ticks="slider.sliderTicks()" ticks-labels="slider.sliderLabels()" formatter="slider.myFormatter" tooltip="always"></slider>
                <p>Value: {{slider.sliderValue}}</p>
                <p>VFG: {{vfg}}</p>
            </div>
        </div>

Now, I have to bind the slider min and max value like {{sliderMinValue}} and {{sliderMaxValue}} instead of static values. But when I do this, I am getting this type of error as shown in below figure:

在此处输入图片说明

Plunker: https://plnkr.co/edit/mSuWAtL89DEDE88pwAm5?p=preview

Try to change it in order to use ng-min and ng-max :

<slider ng-model="slider.sliderValue" 
        ng-min="slider.sliderMinVal" 
        ng-max="slider.sliderMaxVal" 
        step="1" 
        value="sliderApp.sliderValue" 
        ticks="slider.sliderTicks()" 
        ticks-labels="slider.sliderLabels()"  
        formatter="slider.myFormatter" tooltip="always">
</slider>

Fork of your Plunker

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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