angularjs routing based on conditions

Is there a way to pass a hidden value or optional parameter that is not part of the URL in order to switch between states based on that parameter? I attempted something like this:

<a ui-sref="state2({key:'value', optional: 'B'})">Send param and go to page 2</a>

In the route provider:

$stateProvider
  .state('state2', {
  url: "/:key",
  templateUrl: "state1.html",
  params: {
    optional: null
  },
  controller: 'contollerA'
})
  .state('state3', {
  url: "/:key",
  templateUrl: "state3.html",
  controller: 'contollerB'
})
  .state('state4', {
  url: "/:key",
  templateUrl: "state4.html",
  controller: 'contollerC'
})

In the controller:

.controller('contollerA', function($scope, $stateParams, $state) {
    if ($stateParams.optional === 'B') {
        $state.go('state3');
    } else {
        $state.go('state4');
    }
})

Unfortunately, this setup is not working as expected and the browser console shows an error message saying "invalid state reference." What is the correct approach to achieve this?

Answer №1

The reason state2 is lacking the "key" parameter in configuration, resulting in the need for a replacement.

.state('state2', {
  url: "/:key",
  templateUrl: "state1.html",
  params: {
     optional: null
  },
 controller: 'contollerA'
})

Here's the updated version:

.state('state2', {
  url: "/:key?optional",
  templateUrl: "state1.html",
  params: {
     optional: null,
     key: null
  },
 controller: 'contollerA'
})

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

Navigate to all hyperlinks in browser without the use of jQuery - specifically for Firefox OS

I stumbled upon this interesting solution on a programming forum. I'm curious, how does this code work without relying on jquery? $('a[href^=http]').click(function(e){ e.preventDefault(); var activity = new MozActivity({ name: ...

Using javascript, what is the best way to clear a text field with a specific condition?

When I clear the text field #t1, I expect text field #d1 to also clear. However, the last two lines of code do not achieve this result. function utl() { var tField = document.getElementById('t1'); var dField = document.getElementById(&a ...

Best Practices for Managing Asynchronous Updates in Angular Controllers

Let me explain my current setup -- I have a controller that utilizes a service to perform some tasks and then fetches data asynchronously. Right now, the data is returned after a timeout, but ideally it would involve more complex operations: This is how m ...

Accessing a variable outside of the function call in AngularJS is not possible

After starting to tackle AngularJS, I've encountered an issue that's been plaguing me. It seems like I'm unable to access the data returned by $http.get() outside of the method call. Here's a look at the code snippet: (function(){ ...

Opting for a GET request over a POST request when interacting with a JSON RPC API

Can I send a GET request to a JSON RPC API? I'm attempting to do this with the random.org api (). It currently works with a POST request, but I need to use GET requests for all the APIs in my project. Below is the working POST request: function getNe ...

Tips for comparing and adding a field to a sub-array within an object

I have a scenario where I have two objects. The first object contains name and id, while the second object has some fields along with the id field from the first object. For Example: FirstObj = [{ _id: '48765465f42424', Name : 'Sample& ...

Vue Router generates a URL containing %2F when dealing with slug routes

I am facing an issue in my Vue 3 application where I need to create a route for dynamic slugs. Currently, when using RouterLink, the generated URL contains %2F instead of actual slashes which is not the desired result. For example, the current URL looks l ...

Matching exact multiple words with special characters using Javascript Regular Expression

My issue involves using RegExp to match multiple words with dynamic values. Whenever a special character like "(" is included, it interprets it as an expression and throws an Uncaught SyntaxError: Invalid regular expression error. let text = 'worki ...

Three pie or doughnut charts instead of one are utilized within Chart.js

Can multiple charts be nested inside one another? To see an example of a single chart and what I would like - where the first one is placed inside the second one and so on - please refer to this js fiddle. var data = [ { label: "title 1", value: ...

I have successfully implemented an onChange function with its corresponding set of parameters. However, I now desire to extend its functionality by incorporating

I have an onchange function that triggers when the "pending" option is selected in a select dropdown menu. This function adds a predefined value to an input field. However, I also want this functionality to apply when the page loads. Currently, the "pendin ...

Connecting two tables in an express API

Currently, I am in the process of developing an API using Express.js. At this stage, my initial tests are functioning correctly. My goal now is to retrieve values from two separate tables. For example, consider the following 2 tables: Table_A Id: 1, Name: ...

Issue with Google Maps Angular directive failing to function correctly

Trying to implement the Google Maps for AngularJS directive within a function has been a challenge. I've managed to make it work by moving $scope.map outside the function call and setting the lat/lon statically. However, my goal is to dynamically set ...

Select the radio button that is hidden from view

There is a radio button with a hidden input that I can't see when inspecting the element. This is what I am encountering: https://i.stack.imgur.com/LnKnz.png Here is the HTML code snippet: <rhr-radio-button element-id="criterion-type" model="$ct ...

Scan across a lineup of pictures

I am looking to showcase a series of images in a horizontal alignment, but I want to avoid overloading the screen width. My goal is to allow users to navigate through the images using their mouse - when they move right within the image container, I want t ...

Guide on separating a variable by commas using jQuery

After making an Ajax call to a Java servlet, I am retrieving data and storing it in the 'data' variable upon success. Here is the code snippet: var s1=""; var ticks =""; $('#view').click(function(evt ...

Guidance on displaying values from one PHP file in another PHP file

Is it possible to achieve this scenario? I have two files, named main.php and submenu.php. In main.php, I have the main menu defined as follows: In another file called submenu.php, I have a list structured like this: <ul> <li>1</li> &l ...

Converting an enum into a key-value array in TypeScript: A simple guide

var enums = { '1': 'HELLO', '2' : 'BYE', '3' : 'TATA' }; I am seeking a solution to convert the above object into an array that resembles the following structure, [ { number:' ...

When using Axios to GET from a local PHP file, it only retrieves the code instead of running

I've run into an issue accessing the API as it has CORS disabled, requiring me to make requests on the server side. Currently, I'm using React and Axios to send a GET request to a local php file that should trigger cURL execution. However, instea ...

Issue with swal() not triggering in Internet Explorer 11

Looking for some assistance here, I believe I might be missing a small detail. The _layout.cshtml file includes all the necessary scripts to ensure that sweetheart works on IE: (this used to work in previous versions, but we haven't had to support I ...

I'm having trouble with my Laravel edit page not functioning properly when using vue.js. Can anyone help me troubleshoot

Currently, I am developing a dashboard to display details. Users can click on the edit button to modify their information. However, when I try to edit by clicking the button, nothing happens. It seems like the editing feature is not functioning properly, a ...