Md-slider, how to know when it was finished?

Asked

Viewed 61 times

2

I have this slider:

<md-slider step="5" min="15" max="120" ng-model="tempo" aria-label="red" id="red-slider" class="">
</md-slider>

I could not find a legal way to know when the user changes the slider, I worked this way:

$scope.tempo = 25;
$scope.tempo_final = '15 Min';

$scope.$watch(
    function(tempo) {
        console.log($scope.tempo);
    }
);  

But the return of this function is not interesting, even more that I will throw this value in bank, so would have some way to know when this slider is amended?

Use the Angularjs v1.3.15 if you specify another version for this method, I can change.

1 answer

1

I’ve never tested with Angular Material, but if the logic is the same, you can use a ng-blur or ng-change. Both perform a function after user interaction with the field.

The difference is that the ng-blur is executed after an interaction with the field and the ng-change is executed when the field value is modified. Both can call a function within your controller, as follows:

<md-slider step="5" min="15" max="120" ng-model="tempo" aria-label="red" id="red-slider" class="" ng-blur="minhaFunc(tempo)">
</md-slider>

or

<md-slider step="5" min="15" max="120" ng-model="tempo" aria-label="red" id="red-slider" class="" ng-change="minhaFunc(tempo)">
</md-slider>

And on your controller:

$scope.minhaFunc = function(tempo) {
    console.log(tempo);
};

I hope it helps.

Edited:

Take a look at this codepen that extracts directly from Angularmaterial and just added the function of ng-change, it worked normally. http://codepen.io/anon/pen/gPbzRV

Change the slider and see what new value will be written on top of it.

  • Already tested this way, it does not call the ng-change method :(

  • Nor with the Blur?

  • It also does not accept.

  • You have to see how the Material deals with this type of interaction. But what is the return of the $watch you got?

  • @Shelon updated my answer, see the direct example of Angularmaterial, it is working.

  • In this example if I remove the change and direct print the ng-model time the action will be the same. I didn’t use the watch because it keeps following every move of the slider, that is when I change the number 20 to 150 it does 130 actions theoretically, so it is not feasible for the application I am trying to do. Just need it to show the final value, when the user finished the interaction with the Object, in Dragand.

  • Yes, I did. But did you see that I updated the answer? There it is working. There must be something in your code blocking ng-change, or you didn’t set it right. Have you checked there?

  • Yes, but in this example he sends every time the number is changed a request, that is while dragging the slider, he would send to me the number that is passed in the slider. I need you to send it when I stop the slider, not when I’m changing it.

  • @Shelon Switch to ng-blur, see: http://codepen.io/anon/pen/obgVMG

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.