Trouble with directive causes Angular Slider Touch functionality to fail

Currently, I am implementing a directive in my application

https://github.com/prajwalkman/angular-slider

Most of the functionality is working fine except for touch support when trying to drag the sliders on the screen. Touch support seems to work on the provided examples but not in my application. I have verified that I am using the same version of angular, etc.

Here is the relevant code snippet from my application:

filter.js (Controller)
$scope.lower_price_bound = 50;
$scope.upper_price_bound = 3000;

HTML

<slider floor="10" ceiling="3000" ng-model-low="lower_price_bound" ng-model-high="upper_price_bound"></slider>

Do I need to make any additional adjustments to enable touch support for mobile devices?

Thank you!

Answer №1

For those still facing this issue, a potential solution can be found here: JS: Understanding the event.touches property. It seems that jQuery may not be passing the touches property in its custom event object.

This adjustment worked for me:

onMove = function () {
              var eventX, newOffset, newPercent, newValue;
              eventX = event.clientX || event.touches[0].clientX;
              ...

Change it to:

 onMove = function () {
              var eventX, newOffset, newPercent, newValue;
              eventX = event.clientX || event.touches[0].clientX;
              ...

(located on line 227 in my version of angular-slider.js)

Answer №2

It appears from the slider directive code that there is no specific need for additional setup to enable touch event support. I have created a plunker example here using the provided code, and it functions correctly. Testing was conducted on my Sony Xperia phone using Google Chrome.

Answer №3

After implementing the suggestions from Ryan above, I successfully resolved the issue with my sliders. However, it seemed to cause a problem in Firefox as well, as noted in the comments. To address the Firefox compatibility issues, certain modifications need to be made within angular-slider.js:

Line 79:

Modification:

sliderDirective = function($timeout) {

Add a $window parameter:

sliderDirective = function($timeout, $window) {

Line 227:

Modification:

onMove = function () {
    var eventX, newOffset, newPercent, newValue;
    eventX = event.clientX || event.touches[0].clientX;
    ...

Include a customEvent parameter in the onMove() function and assign a custom event variable (myEvent):

onMove = function(customEvent) {
    var eventX, newOffset, newPercent, newValue,
        myEvent = $window.event || customEvent;

    eventX = myEvent.clientX || myEvent.touches[0].clientX;
    ...

This adjustment allows the function to utilize the native window.event variable in Firefox or the one provided by the library.

Line 317:
Finally, update the dependency declaration by replacing:

qualifiedDirectiveDefinition = ['$timeout', sliderDirective]; 

with $window included:

qualifiedDirectiveDefinition = ['$timeout', '$window', sliderDirective];

Answer №4

Another option to consider is using ng-slider

This plugin enables touch device users to simply click on the toggle instead of having to drag, which can be challenging on touchscreens.

Additionally, ng-slider provides a wide range of features and customization options to enhance user experience.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Generating XML format from JSON using JavaScript

I am looking to create an XML format based on my JSON data, rather than converting it from JSON to XML. Here is an example of the JSON I want to convert to XML: var jsonData = { "Smart Shoes":{ "Product":"Smart Shoes", "Price":24.99, ...

Problems with Ajax calls are preventing Internet Explorer9 and Internet Explorer10 from functioning properly

I am facing an issue with displaying item pages using Ajax. The function works perfectly in Chrome, but not in Internet Explorer. <script type="text/javascript"> function obtainInfo(str) { if (str=="") { document. ...

Create automated scripts with Selenium using the programming language JavaScript

Is it possible to create selenium webdriver scripts using only javascript? If so, what are the benefits of choosing javascript over languages like java or C#? In what situations would javascript be the preferred option? Appreciate your insights. ...

What is the method to allocate the smallest available number that has not yet been assigned?

As I'm dynamically generating elements and adding them to the page, each element needs a unique numerical id. However, due to non-linear removal of elements, there can be gaps in the assigned ids. For example... In one scenario: 1, 3, 4, 5, 16, 22. ...

What causes the Invariant Violation: Invariant Violation: Maximum update depth exceeded error to occur when using setState()?

Looking for some help with this component: // here are the necessary imports export default class TabViewExample extends Component { state = { index: 0, routes: [ { key: 'first', title: 'Drop-Off', selected: true }, ...

Capture the response from an AJAX request and store it in a JavaScript variable

I've been struggling to find a solution for this issue for quite some time now without any success. Here's what I'm trying to accomplish: I need to retrieve an array from my PHP file so that I can utilize it in my JavaScript code. Example. ...

Encountering a surprising token error while running a node.js application with a classic example

I downloaded node.js from its official website and followed the instructions provided here. I attempted to run the example code snippet from the "JavaScript - The Good Parts" textbook: var myObject = { value: 0; increment: function (inc) { this.value ...

Why does the MEAN Stack continue to route using the '#' symbol in the URL?

Currently navigating the realm of back end development. Started off by following a guide on express from thinkster. In need of some clarification. Initially, I grasped that front-end and back-end routing serve different purposes. Front-end routing relates ...

Exploring the FormData Object in Mongoose

Currently, I am in the process of developing a geolocationAPI that would allow users to query locations based on their specified minimum and maximum distance. However, I am facing difficulties in retrieving these values within my controller. Here is a sni ...

Converting multipart/form-data to multipart/mixed: A step-by-step guide

Looking for guidance on utilizing multipart/mixed content-type in the document insert REST api (POST /v1/documents) rather than multipart/form-data. Can someone point me to a resource or example that demonstrates this? Thank you. For context, I am using A ...

How can I avoid using the appendTo method in jQuery?

There seems to be an issue in my HTML code, could you please take a look at the code snippet below: HTML: <div class="agent_select_wrap"> <select name="menu-114" class="wpcf7-form-control wpcf7-select" aria-invalid="false"> <op ...

Discovering the art of line breaks

Imagine I have a random block of text displayed in a single line, like this: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Due to various reasons such as width settings or text-zoom, the text may display as two or more lines on the viewer&apos ...

Using tabs within a Dialog prevents the Dialog from adjusting its height automatically

Solved: Find the solution in the comments section below. I recently built a Tabs feature within a Dialog box, but ran into an issue where the height of the Dialog did not match up with the height of the tab. The problem arose when a form was placed insid ...

The Material UI Drawer is fading into the background instead of smoothly sliding into view

Seeking guidance as a novice in SD, I am dedicating my free time to honing my skills. Any advice is appreciated, but please explain it as if I were a child. My current endeavor involves making the Drawer Component feature functional and customizing it to s ...

What is the most effective way to bring in "server-side only" code in Next.js?

I am currently working on my index page's getServerSideProps function and I want to utilize a function called foo, which is imported from another local file. This function relies on a specific Node library that cannot be executed in the browser becaus ...

When executing code in React JS, I encountered no errors, but the output did not match my expectations

I am facing a challenge with running the Hello World program in React JS using webpack. Attached below is the project structure for reference: https://i.stack.imgur.com/tQXeK.png Upon executing the npm run dev command in the CLI, the browser launches bu ...

Using Ember's transitionTo method and setting a controller attribute within a callback

I am working with Ember code to transition to a specific route while also setting controllerAttr1 on 'my.route' Upon transitioning to "my.route" using this.get('router').transitionTo("my.route"), I have set up a callback function that ...

Executing simultaneous asynchronous queries with Mongoose results in an error: `SyntaxError: Illegal return statement` due to

My goal is to make multiple asynchronous requests to Mongo using Mongoose in parallel. To achieve this, I created a helper function that handles these queries dynamically without knowing the exact number beforehand. While my current implementation works ...

Transfer data from distinct arrays to separate variables

I have 2 arrays structured like this: let dataA = [{"id": "a1", "name": "Alpha"}, {"id": "a2", "name": "Beta"}, {"id": "a3", "name": "Gamma&quo ...

Guide to initiating a node.js socket.io server through a brackets extension

I am currently working on developing a brackets extension that involves sending data to a server. What I aim to do is execute a server.js file from my main.js file, which will create a node.js socket.io server. Once this server is set up, the extension sho ...