Enhancing the visual display of a webpage using AngularJS

Hello there! I'm currently working on enhancing an angular 1.6 app and have encountered a dilemma that needs solving. Let me provide some context: The page in question is a lengthy form consisting of thirty questions. The client-side logic includes numerous conditional statements, for example, if the user selects “Yes” on Q1, then display Q2-4, but if they choose "No" on Q1, skip to Q5. This pattern repeats throughout the extensive form.

Now, here's where things get interesting—what happens if a user mistakenly selects “Yes” on Q1, proceeds to answer Q2-4, and then decides that Q1 should actually be “No”?

So here are my two burning questions: 1) How can I effectively reset or clear the models? And 2) how do I update the view accordingly, ensuring that if they revisit Q1 choosing "Yes", Q2-Q4 are returned to their initial state?

Regarding the first issue, I've devised a simple “clearAll” method as shown below:

function clearAll(arr){
        arr.forEach(function(element){
            if(element!==undefined){
                 element = null; 
            }    
        });
    }

Although this function clears the models, it falls short when it comes to updating the view. How can I ensure that Q2-4 are visually cleared/blank?

My current idea involves updating the view via:

$scope.$watch( myModel, function(val){
    //set to null or delete?
}

However, I am struggling to generalize this solution to make it flexible enough to accommodate an array. Any tips or suggestions would be greatly appreciated. Thank you!

Answer №1

To tackle your specific issue, my recommendation would be to utilize ui-router and resolve different states depending on a question. For example, you could have a state called state.questions where different parameters are taken into account. If your base intro page has the URL /questions (perhaps welcoming users to the questions section), then subsequent URLs like /questions/1 would represent the first question, /questions/2 the second question, and so on.

let questions = [];

This list of objects can be stored in a simple service throughout the application. It can later be populated with objects such as:

{ 
q1: {
  answers: [1,2,3]
},
{
q2: {
  answers: [1,3]
}

You have the option to clear this list once it's no longer needed or add new answers to it. Additionally, you can check for a specific answer before loading the state, offering you the flexibility to achieve your desired outcome.

If you're considering a less elegant solution, you might explore using $watch with a watch group and timeout in the following manner:

$scope.$watchGroup(arrayOfModels, (newVal, oldVal)=>{
  if(newVal) {
   // Perform the desired actions with the models here
   // If they don't take effect, consider using $timeout(()=> $scope.$apply());
  }
})

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

Is there a tool or software available that can securely encode a text file into an HTML file without the need for loading it using AJAX?

At the moment, I'm using jQuery to load a txt file (in utf-8) via $.ajax. The txt file has some break lines, such as: line1 line2 line3 When loaded through AJAX into a variable, it appears as: line1\n\nline2\nline3 I could manuall ...

Calculate the total of all values associated with a dynamically generated key within an array of objects

I'm trying to calculate the sum of similar keys in an array of objects. Each object in the array will have some common keys, but not all arrays will share the same set of keys. I'm considering storing these keys in a separate array and then loopi ...

Is there a way to turn off the pinch-to-zoom trackpad gesture or disable the Ctrl+wheel zoom function on a webpage

Take a look at this example - While zooming in on the image by pressing ctrl + scroll, the image zooms but the page itself does not scale. Only the image is affected by the zoom. I am attempting to replicate this functionality on my Next.js page. I have ...

Evaluation of Google Closure Library's performance

When researching the performance of JavaScript libraries, I come across numerous websites that compare the speed of popular libraries including: jQuery (known for being slow) Prototype (especially sluggish in IE) Dojo (considered the fastest when it come ...

Tips for adding a sequence of numbers to a database using Angular

How can I add a range of numbers to a database using a form? <form> <input class="form-control"></input> <!-- enter first number --> <input class="form-control"></input> <!-- enter last number --> ...

jQuery deduct a value from a given value

i have a full duration that is divided into multiple sections, the user needs to input the duration for each section i would like the system to calculate and display the remaining duration from the full duration, regardless of whether the user fills out ...

Retrieve container for storing documents in JavaServer Pages

Previously, I created JSP and HTML code to upload a file from the hard disk into a database using <input type="file" name="upfile"> However, a dialog box with an "Open" button is displayed. What I am looking for is a "Save" button that will allow u ...

vee-validate remains consistent in its language settings

Having trouble changing the error message in vee-validate, any suggestions on how to fix this? import VeeValidate from "vee-validate"; import hebrew from "vee-validate/dist/locale/he"; Vue.use(VeeValidate, { locale: "he", dictionary: { he: { me ...

Determine the vertical dimension of an element through a JavaScript event listener

I've been working on creating an Apple-style image sequence scroller from a codepen demo. Here's the link to the original: https://codepen.io/jasprit-singh/pen/LYxzQjB My goal is to modify the JavaScript so that the scroll height is based on a p ...

Clickable link in popup window using fancybox

When I try to use fancybox to open an iframe and scroll to an anchor tag, it works perfectly in IE but not consistently in other browsers. It stops at a different place than the anchor. I suspect the issue may be related to JavaScript based on information ...

What is the best way to trigger the keyup event in that moment?

Currently, I am using regex to validate the name field. Each time a key is pressed, a validation function is triggered. If the input matches the regex pattern, I need to empty out that specific character from the input field. However, when previewing the p ...

Ensure that the loop is fully executed before proceeding with any additional code

Check out this code snippet I've been developing: let arrayB = []; for (let index = 0; index < res.length; index++) { let fooFound = false; const dynamicFoo = require(`./modules/${res[index]}`); rest.get(Routes.applicationCommands("BLA ...

Is there a way for me to update the button text and class to make it toggle-like

Is there a way to switch the button text and class when toggling? Currently, the default settings show a blue button with "Show" text, but upon click it should change to "Hide" with a white button background. <button class="btn exam-int-btn">Show< ...

Submitting a form using Ajax that was generated with the help of jQuery

Using a table with various rows, each row has an edit button. Upon clicking the edit button, a form is generated using ajax/json to populate the form data based on the selected row. An issue arises when setting up the ajax for this form. Although the met ...

Necessary within a JavaScript Class

As a newcomer to using classes in JavaScript, I've been exploring the best practices and wondering about how 'requires' work when used within a class. For example, let's say I want to craft an IoT Connection class for connecting to the ...

Experiencing a problem with my JavaScript code in Node.js due to an asynchronous issue arising when using a timeout

My goal with this code is to retrieve JSON questions and present them to the user through the terminal. The user has 5 seconds to respond to each question before the next one appears. However, I encountered an issue where the next question times out automa ...

The UI components are failing to render properly, despite having the correct states defined

Similar post found at this link. Working on a project with angular and utilizing ui router for managing multiple views. However, encountering an issue where the views are not rendering upon launching the application. Here is my router configuration: app.c ...

Harnessing the power of JSON within an HTML document to display dynamic data

Check out the JSON data through this link: The JSON file contains information about the weather. I need assistance with incorporating the data from the provided link into my HTML document as I am new to utilizing JSON for this purpose. <html> < ...

Passing an empty JSON object through Ajax requests

To Whom it May Concern (I am simply attempting to meet the "please add more detail" requirement) Upon sending data to the server as shown below, the body appears empty. Server // Route for POST method app.post('/pass', function (req, res) { ...

Variable missing in the ExpressJs view

Hey there! I'm new to Nodejs and currently experimenting with it. I've been trying to convert some of my basic Python codes to JavaScript. In one of my projects, I am sending a get request to the YouTube API and receiving 50 results in JSON forma ...