Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8808283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:35:02+00:00 2026-06-14T02:35:02+00:00

I have the following directive: directive(‘yearSlider’, function(Filter) { return { restrict: ‘E’, // must

  • 0

I have the following directive:

directive('yearSlider', function(Filter) {
    return {
        restrict: 'E',          // must be an element
        transclude: false,      // don't preserve content
        controller: function($scope) {
            $scope.$watch('yearMin + yearMax', function(newVal, oldVal) {
                if(newVal === oldVal) return; // skip initialization

                $("#year-slider").slider("option", "min", parseInt($scope.yearMin));
                $("#year-slider").slider("option", "max", parseInt($scope.yearMax));
                $("#year-slider").slider("value", $("#year-slider").slider("value"));
            });
        },
        link: function postLink($scope, $element) {
            $element.slider({
                range: true,
                min: 1900,
                max: new Date().getFullYear(),
                values: [ 1900, new Date().getFullYear() ],
                slide: function(event, ui) {
                    $scope.yearFrom = ui.values[0];
                    $scope.yearTo = ui.values[1];
                    $scope.$apply();
                },
                change: function(event, ui) {
                    $scope.yearFrom = ui.values[0];
                    $scope.yearTo = ui.values[1];
                    Filter.apply($scope);
                }
            });
        },
        template:
            '<div id="year-slider"></div>',
        replace: true
    }
}).

This directive is used in a view and it renders a slider correctly.

I’m assigning both values of the slider to two models yearFrom and yearTo to get the range of years specified by the slider. In some cases, I need to change the min/max values of the slider and that’s what I’m doing in the controller above.

The issue here is that I’m watching for both values yearMin and yearMax and these two models change consecutively which leads to the controller and change of $element.slider executing twice.

What I’m trying to achieve is to execute this once but the fact that I have two models makes it logically impossible.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T02:35:04+00:00Added an answer on June 14, 2026 at 2:35 am

    I figured it out while writing the question, I simply changed my model to:

    $scope.years = { min: data.yearMin, max: data.yearMax }
    

    And changed my $watch to:

    $scope.$watch('years', function(newVal, oldVal) {
        if(newVal === oldVal) return; // skip initialization
    
        $("#year-slider").slider("option", "min", parseInt($scope.years.min));
        $("#year-slider").slider("option", "max", parseInt($scope.years.max));
        $("#year-slider").slider("value", $("#year-slider").slider("value"));
    });
    

    Putting JQuery slider aside, this is a common issue that faced me with AngularJS, particularly having many models firing multiple events to watchers while I needed them to fire once. I believe grouping them in a similar fashion (if applicable) would solve this issue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following element, <button test-directive>value</button> I know I can use the ng-click
I have the following method: public bool IsValid { get { return (GetRuleViolations().Count() ==
I have the following directive on top of my master page <!DOCTYPE html PUBLIC
I have the following function to convert an integer of arbitrary size to a
I have an AngularJS directive that renders a collection of entities in the following
I have the following directive in my .htaccess file which works on one server,
I have the following location directive in my nginx config file: server{ ... location
I have seen the following #include directives: #include <xstring> #include <cstring> #include <string> #include
Using CentOS5, I have Apache configured with the following directives. Alias /pscript/ /var/www/pscript/ <Directory
I have the following code to scroll images in vertical direction (uses jCarouselLite )

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.