Issue with AngularJS not refreshing due to nesting of directive within another directive

In my AngularJS quiz application, I have two types of questions:

  1. Simple - where you need to choose 1 or 2 choices to answer the question and proceed to the next question.

  2. Range Slider - where you need to drag a range slider. When the user stops dragging, I validate the answers and move on to the next question.

I display the current question using ng-if (current $index = $scope.currentQuestion) and increment it by one every time a question is answered.

While everything works smoothly for simple questions, there seems to be a 1-2 second delay in displaying the next question after a Range Slider question.

To see the next question, I need to click somewhere on the page again.

Could someone please explain why the update is not instant like the simple questions and what causes this delay?

.controller('MainCtrl', function($scope) {

  $scope.currentQuestion = 0;

  $scope.isCurrentQuestion = function(index) {
    return $scope.currentQuestion === index;
  };

  $scope.$on('question:answered', function(event, data) {
    $scope.currentQuestion = data.current;
  });

  $scope.questions = [{
    'title': 'test #1',
    'type': 1,
    'choices': ['Choice #1', 'Choice #2', 'Choice #3', 'Choice #4'],
    // other stuff
  }, {
    'title': 'test #2',
    'type': 2
      // other stuff
  }, {
    'title': 'test #3',
    'type': 2
      // other stuff
  }];

})

http://plnkr.co/edit/EiymOBRcpxoDQQlnHjqV?p=preview

By the way, I'm utilizing https://github.com/angular-ui/ui-slider directive for the range slider.

Answer №1

Typically, these types of errors arise when the $scope.$apply() function is not invoked at the appropriate moment. It appears that the stop() callback fails to call $apply. To resolve this issue, consider modifying your code as follows:

    function stop(event, ui) {
      scope.$apply(function() {
        scope.value = ui.value;
        validate(scope.value);
      });
    }

This adjustment should rectify the unexpected behavior. You can view the updated version here: Forked plunk

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

Find strings that contain some text other than Angular expressions using Regex

I recently discovered a need to identify strings in our projects that are not AngularJS expressions due to expanding into multiple languages. This includes any string that is not completely enclosed in curly braces. My goal is to create a regex pattern th ...

Position items within the dynamically generated div without appending them

Utilizing JavaScript, I dynamically generate a line but struggle to position two balls at both the 1/3 mark from the beginning and end. For reference, please view the image below. I aim to have two balls appear upon pressing enter in the input box. Despite ...

Executing a jQuery script on various elements of identical types containing different content (sizes)

I am currently working on a jQuery script that will center my images based on the text block next to them. Since I am using foundation 5, I have to use a jQuery script to override the CSS instead of using vertical-align: middle. The script looks like thi ...

Changing the Value of an Input Element Dynamically in React: A Step-by-Step Guide

In a scenario where I have a component that takes an element, such as <input />, and I need to update its value programmatically after 15 seconds. Initially, I had the following approach in mind: const MyComponent = (myInput: JSX.Element) => { ...

`How can we efficiently transfer style props to child components?`

Is there a way to pass Props in the Style so that each component has a unique background image? Take a look at this component: countries.astro --- import type { Props } from 'astro'; const { country, description } = Astro.props as Props; --- & ...

Using both PHP and jQuery to add a class upon changing the URL

I am struggling to implement sorting functionality on my webpage, as the active class is not being added to the selected sorting option... Check out my code snippet below: <ul class="nav nav-tabs"> <li class="menusel active" name="hlavni" on ...

Having trouble with implementing the .addclass function in a dice roller project

I'm looking to have the element with id=die load initially, and then on a button click event labeled "click me," apply the corresponding CSS class such as 'die1,' 'die2,' and so forth. function roll() { var die = Math.floor(Ma ...

Identify when the input loses focus in a link directive

I have a query regarding an input that is using a link directive. Specifically, I am wondering if it's feasible to identify when the input loses focus within the link function of the directive. Below is the directive in question: appDrct.directive(& ...

Using the Javascript Foreach method to format dates results in displaying the identical date each time

Hello everyone, I have been working on converting dates from my API using the moment.js library. The sample collection of dates retrieved from the API looks like this: However, I am encountering an issue where all dates are returning as 2019-12-13, despi ...

Secure login system featuring self-contained HTML and Javascript functionalities

I want to create an idle game that will save data on the server instead of using cookies. I am looking for a straightforward method (ideally without using MySQL or PHP) to implement user registration and login functionality using HTML5 and/or Javascript. ...

Creating a FadeIn animation for a hidden div element by using the display:none property

Struggling to implement a fadeIn function for a div tag with a display:none property. How can I make the div tag visible and smoothly fade in while still keeping the display:none property? Attempted solution so far: <div class="graphs_line_based cle ...

Tips for adjusting the file name and extension when saving a text file from a data URI

When I try to download data using a Data URI (this method works in Chrome and Firefox), I use the following code: url1 = "data:attachment/plain;base64,encodeuri(<base64data-xyz>)" By specifying the MIME type as attachment, it forces the file to dow ...

What is causing the redirect to continue even after calling preventDefault() during form submission?

I've been struggling to figure out why my form submission using jQuery keeps redirecting after I submit it. Can anyone help me find the mistake in my code? Here is a simplified version of my form: <form action="http://xxxxxxx" method="post" id="m ...

Is there a method to fetch a product id using JavaScript?

In my current Rails project, I am tasked with creating a feature where users can embed a widget onto a product page of their own website. This widget would be in the form of a script tag. The widget should prompt another window to open with a form that is ...

JavaScript: accessing elements using their unique identifier

It's been a while since I've dabbled in Javascript, so please excuse me if my question sounds silly. I'm attempting to generate an image, assign it an ID, and then retrieve the element using 'getElementById', but I always end up w ...

Edge Browser does not support PHP Websocket technology

I created a multiplayer card game and incorporated a websocket for functionality. To integrate the websocket in php, I utilized this specific library After deploying it on my Ubuntu server, the program functioned smoothly on Chrome and Firefox (The fronte ...

Utilize Angular.js to effortlessly convert a string containing special characters and numbers into an array, and seamlessly display it using ng

I am a beginner in angular.js and I'm facing an issue with converting a string into an array and displaying it using ng-repeat. I found a custom filter from a stackoverflow question and here are the codes: JS: var app = angular.module('globalMo ...

send the user to a different page while transferring post data using Javascript

Currently, I have an HTML form that utilizes the post method to send data to a PHP page for processing into a MySQL database. However, before the user submits the form, there is a button that allows them to view a PDF of the entered data on a new page. Whi ...

execute a file in nodejs

I am trying to use nodejs to load test.txt file. var fs = require('fs'); fs.readFile('./test.txt', function (err, data) { if (err) { throw err; } console.log(data); }); The server path is C:\server\test\serve ...

Designing this unique shape using CSS3 to create a sleek drop-down container

My goal is to design something that resembles the following: The challenge lies in my preference to contain it within a single div, considering the drop-down features an inner shadow and a drop shadow. Thank you in advance, and please feel free to ask fo ...