vincent vincent - 5 months ago 73
AngularJS Question

angularjs - Input[type=range] value not updated

I'm having a weird problem with input type range. The value is not updated even if I force change it. I'm using ionic 1 with angular

I got a list of buttons with dates and a input range slider below.

...
<a grouped-radio="date" ng-model="data.date" ng-repeat="date in data.dates" class="button-small" ng-click="updateTimeRange(date)">{{date | date: 'd-MMM'}}</a>
...
<input type="range" id="time" name="time" min="{{data.minTimeStep}}" max="{{data.maxTimeStep}}" step="0.5" value="{{data.timeStep}}"
ng-model="data.timeStep" ng-change="getTime()">
...


on click of the button I'll call the function updateTimeRange() to reset the min and max of the input range below. here is updateTimeRange() function

$scope.updateTimeRange = function (selectedDate) {

var timeobj = _getMinTime(selectedDate); //function to get minimum time for the selected date

//updating the scopes but it does not work
$scope.data.minTimeStep = timeobj.decimal;
$scope.data.timeStep = timeobj.decimal;
$scope.data.time = timeobj.time;

//tried force update the value directly but does not work also.
//document.querySelector('#time').value = timeobj.decimal;
$timeout(function () {
console.log('timeout value: '+document.querySelector('#time').value);
});

console.log(timeobj);
console.log($scope.data);
console.log(document.querySelector('#time').value);
}


here is the console output. which is totally does not make sense.

enter image description here

however when I tried setting manually in the console, and it seems ok.

enter image description here

Answer

i did a hack. I just force update it in a $timer function. I don't know why. but it works for me.

$scope.updateTimeRange = function (selectedDate) {

    var timeobj = _getMinTime(selectedDate); //function to get minimum time for the selected date

    //updating the scopes but it does not work
    $scope.data.minTimeStep = timeobj.decimal;
    $scope.data.timeStep = timeobj.decimal;
    $scope.data.time = timeobj.time;

    //tried force update in the timer
    $timeout(function () {
        console.log('timeout value: '+document.querySelector('#time').value);
        document.querySelector('#time').value = timeobj.decimal;
    });

    console.log(timeobj);
    console.log($scope.data);
    console.log(document.querySelector('#time').value); 
}