Passing various length values in AngularJS using stateParams

I am facing an issue with passing values of different lengths to the same state:

For instance

<li href="#/app/list/value1">

<li href="#/app/list/value1/value2">

Within my state definition, I have the following

.state('app.list', {
  url: "/list/:passedLow/:passedHigh",
  views: {
    'menuContent': {
      templateUrl: "templates/file.html",
      controller: 'ValueListCtrl'
    }
  }
})

Additionally, I am using

 $urlRouterProvider.otherwise('/app/landing');

When I click on the second link (i.e., passing two values), everything works as expected. However, when I try to pass a single value, the "otherwise" rule kicks in and redirects me to the landing page.

Is there a way to handle passing variable length values as $stateParams?

Answer №1

For a functional plunker, visit this link

To provide more details about a parameter, utilize params: {} and specify the parameter as follows:

params : { passedHigh : { value : null, squash : true } },

This setup informs UI-Router to anticipate the presence of the passedHigh parameter. The default value is null and can be modified if needed. Additionally, we indicate that it should be omitted from the url if it remains at the default value - squash: true

Further information can be found in the $stateProvider documentation

Below is a complete state definition:

.state('app', {
    url: "/app",
    templateUrl: 'tpl.html',
})
.state('app.list', {
    url: "/list/:passedLow/:passedHigh",
    views: {
        'menuContent': {
            templateUrl: "templates/file.html",
            controller: 'ValueListCtrl'
        }
    },
    params: {
        passedHigh: {
            value: null,
            squash: true
        }
    },
})
.state('app.landing', {
    url: "/landing",
    templateUrl: 'tpl.html',
})

View it in action here

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

How can I create a computed field in TypeORM by deriving its value from other fields within the same Entity?

My goal is to implement a 'rating' field in my User Entity. Within the User Entity, there exists a relationship with the Rating Entity, where the User has a field called ratingsReceived that eagerly loads all Ratings assigned to that User. The & ...

Using React to Filter cards according to the selected value from an Autocomplete feature

I'm currently developing a React application that utilizes Material-UI's Autocomplete component to filter cards based on selected car brands. These cards represent various models, and the goal is to update them when a user picks a brand from the ...

Using Event Delegation in Practice

I am facing an issue with loading 2 <span> elements from separate ajax scripts onto a webpage upon selection from a dropdown box. Here is a snippet of my code: <span id='room_rate'>1,000</span> // content loaded by one ajax scri ...

Designing a Dynamic Floating Element that Shifts with Scroll Movement

Hey there everyone!, I am currently working on a project in Wordpress and I was wondering if anyone has experience creating a floating widget that moves along with the page as you scroll. Any suggestions on how to achieve this? Would it involve using Javas ...

After the child promise is resolved, have the parent function return a boolean value

I am currently faced with a challenge in my framework where the parent function must output either true or false>. However, I also need to perform an asynchronous operation within that function.</p> <p>How can I ensure that the parent functi ...

Troubleshooting Firefox HTML Positioning Problem (Functioning Properly in IE and Chrome)

After implementing the code below, I encountered an issue. In IE and Chrome, when hovering over the "CODE" section, the div correctly appears at position 100 x 100. However, in Firefox, the div fails to move to the specified position. What changes should b ...

Utilizing a dynamically created Stripe checkout button

Currently, I am attempting to integrate a checkout button from the Stripe Dashboard into my VueJS Project. I have a feeling that I might not be approaching this in the correct manner, so if you have any advice, I would greatly appreciate it. In order to ...

Display a loading progress bar with jQuery AJAX as your single page website content loads

I am currently working on a simple web page layout that consists of a navigation bar at the top and a body wrapper. Whenever a user clicks on a link in the navigation bar, I use .load to load the content of the page into the wrapper div. $(this).ajaxStar ...

Updating React state by changing the value of a specific key

I've been diving into React lately and I'm facing an issue with updating state values for a specific key. Below is my current state: this.state = { connections : { facebook : "http://facebook.com", flickr : null, ...

What is the purpose of adding "/dist" to the library import statement?

Currently, I am in the process of developing a react component library using vite as my primary build tool. After successfully compiling the project and deploying it to the npm registry, I encountered an issue when importing it into my client app. Specifi ...

Modifying script variables using the Chrome console

There is a button on the website that looks like this: https://i.sstatic.net/G7PBF.png Clicking on this button triggers the following script: https://i.sstatic.net/rIdLW.png function count(){ let downloadTimer; var timeleft = 30; downloadTimer = setInte ...

Unusual Behavior of JavaScript for..in and enum

I'm facing an issue with the peculiar behavior of the for..in loop in JavaScript. Here's the structure of my HTML: <div id="quarter_circle_top_left">...</div> <div id="quarter_circle_top_right">...</div> <div id="quart ...

When utilizing Next.js, the router.push feature will automatically scroll the page to the top, even if the scroll option

Incorporating Next.js' built-in internationalisation features allowed me to seamlessly switch my app's language, but there is one specific issue I'm encountering: When I trigger the changeLanguage function, it causes the page to scroll back ...

Loading jQuery on Ajax can be a challenge

I am currently faced with the challenge of working on code that I did not write myself or fully comprehend. The page utilizes AJAX to dynamically load content, and my goal is to manipulate this content. However, when I try to apply jQuery to the dynamic el ...

Troubleshooting the Thinkster MEAN Stack tutorial: Failure to Generate User Input via POST Method

While in the "Getting User Input" section, I encountered an issue when trying to generate a new post by clicking the "Post" button. Despite entering a title in the textbox, no new post appeared. It seems like there is code preventing the post from being ge ...

Observables and the categorization of response data

Understanding Observables can be a bit tricky for me at times, leading to some confusion. Let's say we are subscribing to getData in order to retrieve JSON data asynchronously: this.getData(id) .subscribe(res => { console.log(data.ite ...

Update the CSS styling for elements that are dynamically added to the page using the

Just started using jQuery for the first time and I'm encountering an issue. I'm trying to load content from an external element onto my page, and while it loads correctly, I'm having trouble using normal traversal methods on the loaded eleme ...

Issue with Internet Explorer: Refusing to run javascript included in AJAX-loaded content

While loading AJAX content that includes a javascript function using the jQuery .load function with done() on completion, I am facing an issue. $('#content').load(a, done); function done() { if(pagejs() == 'function') { ...

Navigating with buttons in the Material UI Drawer

I have implemented a Material UI drawer with some modifications. The original code used buttons, but now I want to navigate to a new page when a button is clicked. For example, clicking on the 'INBOX' button should take me to a page '/new&ap ...

Deactivate web security for JavaScript with Watir

While using Watir, I have the ability to access the methods of the iframes on the page: browser.iframes.map(&:contentwindow) # => ["", "", ""] However, if I try to access the iframes using JavaScript within the same context where I executed the W ...