Exploring History Navigation with AngularJS: Leveraging $routeParams and $location

I have been diligently working on developing a question bank and have successfully coded most of the elements. However, I would like to enhance the user experience by implementing browser history for easy navigation through the questions.

If you want to check out the basic setup on Plunker, click here: http://embed.plnkr.co/1F64dDrxVYfD8ScVGgac/preview

Currently, the page loads under /quiz, but my goal is to dynamically change $scope.currentQ based on the URL search parameter:

/quiz?q=1

I have experimented with using $routeParams and $location as possible solutions, but have not been able to achieve the desired outcome. Any assistance would be greatly appreciated!

// Insert your code here

(function() {
    'use strict';

    var app = angular.module('questionbank', []);
    //////////////
    //Directives//
    //////////////
    app.controller('questionbankController', ['$scope', function($scope) {
        $scope.currentQ = 0;
        $scope.guess = [];
        $scope.SBAchoices = ['a', 'b', 'c', 'd', 'e'];
        $scope.questions = questions;
        $scope.prevQ = function() {
            if ($scope.currentQ !== 0) {
                $scope.currentQ--;
            }
        };
        $scope.nextQ = function() {
            if ($scope.currentQ < $scope.questions.length - 1) {
                $scope.currentQ++;
            }
        };
        $scope.submit = function(guess) {
        };
    }]);
    var questions = [{
    questionid: 1,
    question: "What year is it?",
    choices: [
        "2011",
        "2012",
        "2013",
        "2014",
        "2015"
    ],
    answer: "2015",
    reason: "Because it is not yet 2016!",
    category: "test"
}, {
    questionid: 2,
    question: "Which medical school is the best?",
    choices: [
        "Kings",
        "Imperial",
        "St. George's",
        "Barts",
        "UCL"
    ],
    answer: "UCL",
    reason: "Creators are from UCL, do I need to say any more?",
    category: "test2"
}];
})();

Answer №1

Check out this demonstration of navigation using the $location service (link to code http://plnkr.co/edit/PZX9Td4y6EJEnLm2yK4H?p=preview). This approach simplifies the process of implementing navigation based on the following event:

$scope.$on('$locationChangeSuccess', function() {...})

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

Incorporating a loading animation with an AJAX call

I've come across various solutions to this issue, one of the most enlightening being a response on Stack Overflow suggesting to "display it as soon as you initiate the AJAX call" and then "hide the image once the request is complete." But how would I ...

Error message: "Unable to perform Babel on Gulp in Windows 10"

After upgrading my machine from Windows 7 to Windows 10, I encountered an issue with a gulp task I had set up for a project. The task started failing after the upgrade, and I received the following error in the command line: return binding.open(pathModule ...

Understanding XML parsing problems

I've decided to keep the live xml/atom-feed URL private, as it's hosted on LiveJournal. However, I'm a bit confused about how this all works: let XML_URL = "https://users.livejournal.com/<username>/data/atom"; $(function(){ ...

How to determine if an Angular list has finished rendering

I am facing an issue where I have a large array that is being loaded into a ul list using ng-repeat in Angular. The loading of the list takes too long and I want to display a loader while it's loading, but hide it only when the ul list is fully render ...

Are Bootstrap Input groups inconsistent?

Hey there! I've been working on the sign-in example, but I seem to have hit a roadblock. In my local setup, the top image is what I see in my browser after running the code, while the desired layout that I found on the Bootstrap site is the one below ...

The mystery behind React rendering twice, even when React strict mode is disabled

My code is error-free, but React seems to render this component twice for some reason. I can't figure out why. Here is an image showcasing the issue: https://i.stack.imgur.com/hKvug.png Index.js Page : import LandingPage from '../components/Ho ...

The value of the keycode is consistently 229 for all keys

While using the regular textbox on my android device, I encountered some problems which are listed below. 1. The keypress event is not triggered on the android device. 2. The keycode value always returns as 229. What are some possible solutions to resolv ...

What steps should be taken to ensure that my nodeJS server can maintain the identity of a specific user?

Currently, I am in the process of building a mobile application that utilizes Flutter for the front-end and NodeJS for the back-end. Progress has been steady, but I have hit a roadblock while trying to incorporate a lottery feature. The idea is for the se ...

Simplify your code with promises in JavaScript

Running on an API with node v6.3.0, I have the following code that executes two separate promises based on a conditional check for a parameter in a POST request. if (paramExists) { // query database with this condition User.filter(/* utilize param ...

Experimenting with Jasmine for testing an AngularJS service without the need for mocks

I am interested in utilizing Jasmine for writing tests (not unit tests) for an AngularJS service that acts as a wrapper for a REST API I developed on my server. The goal is to ensure that the service call reaches the server without requiring any form of mo ...

When I utilize the three.js GLTFLoader to import a 3D model in GLTF format, I can't help but notice

As a beginner in frontend development, I lack knowledge of OpenGL. I am using the three.js GLTFLoader to import a 3D model, but the result is not satisfying. The preview on the model website looks ugly, and I want to improve the appearance to match the sam ...

Unfortunately, I am unable to utilize historical redirection in React

When an axios request is successfully completed, I want to redirect. However, I am encountering an error that looks like this: https://i.sstatic.net/irTju.png Below is the code snippet: import React, { useState, Fragment } from "react"; import S ...

Unable to resolve the issue within Angular UI modal

Currently, I am facing an issue with displaying a modal using Angular Bootstrap UI. The modal is supposed to resolve items in the controller, but I am struggling to access those resolved items in the modal controller. $scope.showItemsInBill = function(bill ...

What are the steps to incorporate PointerLockControl in Three.js?

Having trouble correctly integrating the PointerLockControl in Three.js? Despite trying various examples, errors seem to persist. I've been importing libraries through the head part like this: <script src="lib/controls/PointerLockControls.js"> ...

Create a query string using JavaScript and combine multiple parameters into a single param

I am facing a challenge where I need to construct a query string in JavaScript and nest various parameters within one of the parameters. In PHP, I can achieve this using the http_build_query function. However, when attempting to do the same in JavaScript, ...

How is the time complexity of merging two sorted arrays to create one larger sorted array determined?

This is my solution to a problem that involves merging two arrays of integers sorted in ascending order into one larger sorted array. The function merges the arrays by comparing elements from each array and appending them to a new array in an orderly fashi ...

Having trouble uploading my confidential npm package to a secure Nexus repository

I have my own personal collection of books and I am looking to share it by publishing an npm package to a private Nexus registry Here is my package.json setup { "name": "@uniqueorganization/the-collection", "version": ...

Updating data in MongoDB with an expiration feature

Despite setting the expireAfterSeconds option to 60 in my Mongodb shell, my new entries in the unlock schema are not being deleted after 60 seconds. db.unlocks.ensureIndex( { "createdAt": 1 }, { expireAfterSeconds: 60 } ) Here is the structure of my Sche ...

Launching URLs from JSON data in the system browser - Apache Cordova

I am facing an issue with opening links from a JSON in the system browser on both Android and iOS devices. The link generated by the function appears as: {{item.addressLink}}, instead of bit.ly/xyzxyz Here is what I currently have: <a href="#" ng-cl ...

The error was thrown at line 800 in the loader.js file of the internal modules

When I ran yarn install in my project folder, I encountered the following error: internal/modules/cjs/loader.js:800 throw err; ^ Error: Cannot find module 'ts-node/register' Require stack: - internal/preload ?[90m at Function.Module._resolveF ...