Setting the maximum value of the slider and implementing bidirectional binding for the slider - a step-by-step guide

Using the example demonstrated here, I have successfully customized the slider as per the specifications.

index.html

<!DOCTYPE html>
<html>
    <head>
      <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      <title>Rating</title> 
        <link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.0/themes/smoothness/jquery-ui.css">     
        <script data-require="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f6979891839a9784d89c85b6c7d8c2d8c6db94938297d8c0">[email protected]</a>" data-semver="1.4.0-beta.6" src="https://code.angularjs.org/1.4.0-beta.6/angular.js"></script>   
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.0/jquery-ui.min.js"></script>           

        <script src="test.js"></script>   
    </head>

    <body ng-app="myApp">       
        <div ng-controller="testController">                    
                <table>                 
                    <tr>
                        <td>Amount:</td>                                        
                        <td><div slider ng-model="amount" style="width:200px;"></div></td>
                        <td><input type= "text" ng-model="amount" id="txtAmount"></td>
                    </tr>                                                       
                </table>                                            
             </div>      
    </body>
</html>

test.js

var myApp = angular.module("myApp", []);
myApp.controller("testController", function($scope, $http){

    $scope.amount = 1;

}); //end controller

myApp.directive('slider', function() {
      return {
        restrict: 'A',
          scope: {
              ngModel: '='
          },
        link: function(scope, elem, attrs) {

          console.log(scope.ngModel);

          return $(elem).slider({
            range: "min",
            animate: true,
            value: scope.ngModel,
            slide: function(event, ui) {
              return scope.$apply(function(){
                scope.ngModel = ui.value;
              });
            }
          });
        }
      };
    });

The issue arises when:

a) Setting the maximum range beyond 100 seems to be a challenge. How can I make the minimum value 1 and the maximum value 1000?

https://i.sstatic.net/vchrC.png

b) Two-way binding does not seem to be functioning properly. For instance, if I set the value of "txtAmount" to 50, the slider should reflect that value (50)

https://i.sstatic.net/uzjLJ.png

Answer №1

  1. To adjust the maximum range, simply utilize the max option, as demonstrated in the example below. If no value is specified for the max field, it will default to 100.
  2. Angular elements support two-way binding by monitoring changes, so you should implement a similar approach for jQuery elements as well.

Code:

scope.$watch('ngModel', function(val) {
  $(elem).slider("value", val);
});

View the full code on jsFiddle - http://jsfiddle.net/zv78jsz6/

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

Error: We are facing an issue with new.mongoose.Schema as it is not functioning properly and

I am experiencing an issue with my product.js file. It keeps throwing an error about an unidentified identifier, and whenever I try to fix one problem, another one pops up. I have been struggling to locate the root cause of this error. ...

Adjusting the Stripe Button to Utilize a Unique Button Class

I have a stripe button on my website and I want to add my custom class to it. I discovered that manually creating the CSS for it can work, but I prefer to maintain consistency across all buttons on my site by using my custom class. No matter what I attemp ...

I need help creating a Google marker using the Maps JavaScript API

I am currently working with the Google Maps API. The map displays the latitude and longitude when a mouse click event occurs. My next step is to add a Google marker to the map. Below is my JavaScript code snippet: <script> function initMap() ...

What is the process for uploading SVG files created by users onto the server?

After dedicating three full days to researching, I have hit a roadblock in my attempts to allow users to upload SVG files to the server on my website. I created an HTML and Javascript platform where users can input dimensions into a form to create a basic ...

Ways to indicate the language of your website to the Chrome browser for the translate bar

I am experiencing a unique situation with my web app where it appears to be in one language but the Chrome translate bar identifies it as being written in Estonian. Despite trying methods such as lang and xml:lang, the Google translation bar does not seem ...

Refreshening a collection of MongoDB documents

My task involves updating a group of documents in mongoDB by sending them to the API as a JSON array. I want to find a way to update all the documents without having to put the Document.update() function into a loop. Any ideas? I've looked into usin ...

The response is dispatched without delay and does not necessitate awaiting

I am facing an issue with waiting for the completion of the getAllData function before proceeding further. let _partnerToken; async function getAllData(dealerId,id){ let partnerToken; var options = { 'method': 'GET', ' ...

Encountering dependency resolution issues while attempting to install Material UI v5

Encountering a series of dependency resolution errors while attempting to install Material UI. The root cause remains unclear despite previous attempts to resolve issues by upgrading to V5 from V4 due to React compatibility reasons. However, additional con ...

Non-functioning Tooltips on Google Charts

Currently, I am working on integrating Google Chart with ASP.NET and linking it to a SQL Server database. I have encountered an issue while attempting to customize the tool tip. Below is the Header Code: <script src="js/jquery/jquery-1.10.2.js" type=" ...

What is the best way to create two divs separated by a forward slash?

I'm trying to create a menu that looks like the image, but I'm having trouble figuring out how to divide the two divs and make them clickable. I've experimented with using SVG and borders on the divs, but I can't seem to get them to wo ...

The issue with running commands in parallel using npm remains unresolved

Within my project folder, I have a package.json file located at the root directory. This JSON file contains separate client and server folders. In the main package.json file, I have defined the following scripts: "scripts": { "server&quo ...

Using SVG sprites from external sources alongside child nodes

I am working on a website that contains multiple SVG icons and an SVG logo that need to be animated. To improve readability, I have implemented a sprite system using an external file. For loading the sprites, I am utilizing XMLHttpRequests at the start o ...

Angular: Refresh mat-table with updated data array after applying filter

I have implemented a filter function in my Angular project to display only specific data in a mat-table based on the filter criteria. Within my mat-table, I am providing an array of objects to populate the table. The filtering function I have created loo ...

Encountering an issue with Next.js React SSR using styled-jsx: unable to access the 'state' property as

I've come across a problem that I can't seem to figure out. Despite my attempts to search for a solution here, I haven't been able to help myself. I'm new to javascript and react, so please bear with me. Issue: I'm using React (1 ...

Execute a JavaScript file stored in the filesystem using Selenium

Having trouble running a YUI js script using js.executeScript Selenium method. The script is being executed by Selenium Webdriver to simulate a "click" on a hybrid mobile app (the button is in a webview). String IncludeYUI = "script = document.createElem ...

Only trigger the function for a single bootstrap modal out of three on the current page

I'm facing a challenge on a page where I have 3 bootstrap modals and I need to trigger a function only for one modal while excluding the other two. $("body").on("shown.bs.modal", function() { alert('modal Open'); //this alert should ...

The method of pausing a function until the result of another function is returned

There is a function named 'updateProfile()' that includes a condition, which checks for the value of variable 'emailChangeConfirm' obtained from another function called 'updateEmailAllProcessing()'. The issue lies in the fact ...

Module specifiers that are considered relative must always commence with either "./", "../", or just "/"

I need help with importing three.js. I have been following the documentation but I keep encountering an error message: Uncaught TypeError: Error resolving module specifier “three”. Relative module specifiers must start with “./”, “../” or “/ ...

Looking for Recommendations in javascript

I have created a responsive container for users with a simple filter that displays a list of users based on their Id. Current let data = [ {Id:'123',Name:'John Doe',Gender:'Male'}, {Id:'213',Name:'W ...

Using pre-compiled values in an Angular directive within an ng-repeat loop

I'm currently working on a directive that should limit the text within an ng-repeat loop to 50 characters and add an ellipses if it exceeds this length. Here is how my ng-repeat is set up: <li ng-repeat="result in topSuggestions.suggestions"> ...