Optional URL segment in the middle of AngularJS UI-Router

I am looking to add an optional url segment in the following example:

url: "/post/:category/:subcategory/:title/{id:int}",

In this scenario, subcategory is not required. It should be able to accept:

url: "/post/sports/football/some-title/10",

as well as:

url: "/post/sports/some-title/15",

without a subcategory. I know I can achieve this using separate states, but is there a specific rule for making only the subcategory segment optional while keeping others mandatory?

Answer №1

.defineState('post', {
    path: '/post/:category/:subcategory/:title/:{id:int}',
    templateFile: 'views/post.html',
    controlUnit: 'postCtrl',
    parameters: {
        subcategory: { collapse: true, data: null },
    }
})

To learn more, please visit the documentation page

Answer №2

Detailed explanation of the solution can be found below

Angular js - How to add default parameter in route-ui?

Below is an example of how we can define such a parameter:

.state('state', {
    url: '/:category/{subcategory:(?:football|tennis|hokey)}/:title/:id',
    abstract: true,
    template: '<div ui-view=""></div>',
    params: {subcategory : { squash : true, value: 'football' }}
})

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

Removing an item from an ng-repeat loop while also dealing with a FormController within

I am encountering a significant issue with form inputs nested within the ng-repeat directive. I require an input that is an array and can vary in size; allowing users to add or remove parts of the form. Within the ng-repeat directive, there is an ng-form ...

Error in MEAN Stack: Unable to access the property 'companyTitle' because it is undefined

I have established a MongoDB collection named joblist in my database. Additionally, I have developed a DB schema known as jobList.js. var mongoose = require('mongoose'); const joblistSchema = mongoose.Schema({ companyTitle: String, jobT ...

Unable to retrieve data from MySQL Database using Express Route

Struggling to understand how to execute a MySQL database query within the promise in my route file. Currently, I am developing a RESTful API for interacting with a MySQL database using GET methods. The technologies being utilized are Express for the backen ...

How to send a Django form using Django REST framework and AngularJS

Feeling a bit lost on how to handle form processing with django, django-rest-framework, angularjs, and django-angular. By using traditional Django techniques, I can generate a model form in my view and pass it over to my template. With the help of django ...

Issues with jQuery compatibility when used alongside HTML and CSS

My coding tool is flagging errors in my JavaScript file, including: -'$' usage before definition. -Incorrect spacing between 'function' and '(' This is the content of my JS file: $(document).ready(function() { $(window).s ...

AngularJS component downgraded without change detection

Currently, I am utilizing Angular's downgradeComponent for performance optimization purposes. You can find more information about it here: https://angular.io/api/upgrade/static/downgradeComponent. The Angular component I am working with is defined as ...

Having trouble accessing the length property of an undefined array while passing it from Node to Jade

Hey there! I'm currently working on rendering jade from node using the following code: router.get("/register", function (req, res) { var countries = [1, 2, 3]; res.render("./account/register", { countries: countries }); }); Below ...

What kind of content can be duplicated but remains untraceable through search engines?

In the past, I utilized the alt attribute of images to generate text that could be copied to the clipboard: The style attribute can conceal text, however, hidden text still remains and can be discovered using methods like indexOf or control-F. Is there a ...

Is there a way to replace jQuery's .submit() method with Angular?

Is there a way to trigger an Angular event similar to the jQuery method shown below? $('.link').click(function () { $('form').submit(); // this will submit form }); It's important to clarify that this is not using the onSubm ...

Creating a flexible grid layout that adjusts to show either one or two columns on smaller mobile screens

NOTE: This is not a duplicate as the layout order for columns and rows is specifically tailored for mobile devices. I am attempting to showcase this grid design on mobile devices with the following sequence: First row: header (1 column, full width) Secon ...

Creating multiple versions of a website based on the user's location can be achieved by implementing geotargeting techniques

It may be a bit challenging to convey this idea clearly. I am interested in creating distinct home pages based on the source from which visitors arrive. For instance, if they come from FaceBook, I envision a tailored home page for FaceBook users. If they ...

Please be patient for the function to complete its execution

I am currently facing a challenge where the execution of functionA needs to halt until functionB() has provided an answer. In this case, the duration for functionB to respond varies depending on user input, ranging from 1 second to up to 10 seconds. As a r ...

Combining MxGraph with AngularJS 1 for Seamless Integration

I recently attempted to dynamically add mxgraph with the client-side library angularjs, but I couldn't find any relevant documentation on how to do so. Can anyone offer guidance on integrating mxgraph, such as which files need to be included and what ...

While attempting to use JavaScript and AJAX to read a JSON object, I encountered an issue caused by the CORS

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Docu ...

Make a CSS div that expands based on the size of its absolutely positioned content

I'm currently experiencing some challenges trying to make a parent div expand to fit its contents which are positioned absolutely. The footer is reaching the bottom of the screen, but not the bottom of the document. This issue is clearly caused by th ...

Why do confirm or alert boxes in Safari on Windows require a double click?

I'm currently working on a simple JavaScript example where I want to display an alert box when an HTML button is clicked in SAFARI. However, I've noticed that it requires a double click to make the alert disappear from the screen. Does anyone ha ...

Extract different properties from an object as needed

Consider the following function signature: export const readVariableProps = function(obj: Object, props: Array<string>) : any { // props => ['a','b','c'] return obj['a']['b']['c'] ...

Issue with Vue 2: Certain browsers not displaying images

In my current project, I am using Laravel 5.4 and Vue 2.4.2. The issue I am facing involves rendering data with the v-for directive from an axios returned array. <div class="card" style="width: 20rem; margin-left:1rem;" v-for="ad in ads" v-cloak> ...

Error: The property 'combine' of 'winston_1.default.format' cannot be destructured since it is not defined

Encountered an error while using Winston in Node.js, how can we resolve it? The version of Winston I am using is 3.3.3 and winston-daily-rotate-file version is 4.5.0 I attempted npm i winston@next --save, but the error persists. ** Here is the Error Mes ...

What are the implications of employing a singular global variable for angular modules?

After coming across a template, I noticed that the variable anApp is declared once in app.js and then shared among all modules. This has led me to wonder: 1. Will using a single variable result in all Angular components being stored in the global scope? 2. ...