AngularJS navigation structure

Here is the route setup I am currently using:

.when('/stories/:action/:assetId', {
  templateUrl: 'sometpl.html',
  controller: 'ctrl'
}

My question is: How can I restrict the 'action' parameter to only accept specific words, such as ['read', 'list']?

Answer №1

To view the solution for handling similar URLs with different controllers in AngularJS, please refer to 18131834.

For a different approach, you can utilize the ui-router library, which allows for regular expressions in route parameters:

$stateProvider.state("artists-index", {
    url: "/artists/{page:[0-9]*}",
    templateUrl: "/www/artists/index.html",
    controller: "ArtistsIndexController"
});

$stateProvider.state("artists-profile", {
    url: "/artists/{name}",
    templateUrl: "/www/artists/profile.html",
    controller: "ArtistsProfileController"
});

Answer №2

If you want to implement this functionality, you can start by retrieving the action in the controller and assigning it to a variable. Once you have done that, you can then proceed to check if the action is included in an array of supported actions. Here's a sample code snippet to illustrate this:

String requestedAction = 'read'; // Action retrieved from URL
String[] supportedActions = ['read', 'list'];
boolean isSupported = false;

for (String action : supportedActions) {
    if (requestedAction.equalsIgnoreCase(action)) {
        isSupported = true;
        switch(requestedAction) {
            case 'read': {
                /* Your custom logic for 'read' action */
            }
            case 'list': {
                /* Additional logic for 'list' action */
            }
        }
    }
}

if (!isSupported) {
    /* Handle the case where the action is not recognized */
}

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

Unexpected behavior with MongoDB update function

If I have a model like this: var stuffSchema = new mongoose.Schema({ "id": 1, "cars": { "suv": [], "sports": [], "supercar": [{ "owner": "nick", "previousOwners": [ ObjectId("574e1bc0abfb4a180404b17f"), ObjectId ...

Unable to logout with ExpressJS passport-local

After pasting the passport-local app into my own, I noticed something interesting: I managed to successfully log in users, but for some reason, I can't seem to get them logged out. app.get('/logout', function(req, res){ req.logout(); r ...

Updating JSON server

Currently, I am utilizing json-server to perform post and get operations successfully. However, I am facing difficulties in updating data. The database contains the following information: { "userRecipes": [ { "id": 1, "desc": "dog", ...

Creating JOIN tables within the create action involves assigning related ids to each table

I am currently working on a room reservation system that involves including options for each room. Data related to the options and their join table, reservation_options, are successfully inserted into the params. However, I am facing an issue with assignin ...

Issue: "TypeError: Unable to retrieve dynamically imported module" encountered while utilizing dynamic imports in Remix application

I am currently facing an issue with dynamic imports in my Remix application. Whenever I try to dynamically import a module using the code below: const module = await import(../lib/lang/lang-${language.toLowerCase()}.js); An error occurs: TypeError: Fail ...

Changing images dynamically using Javascript when hovering over an element

Looking to create a dynamic image switch effect on hover, where multiple images cycle through when hovered over. Each time the mouse hovers over the image, it should switch to the next in a sequence of 5 images. <img id="switch" src="img1.jpg"> $(& ...

Waiting for Angular pages in Selenium with Protractor JavaScript code

I'm currently in the process of testing an AngularJS page and utilizing Selenium (Java) to create automation scripts for it. Below is the code snippet that I've implemented to ensure synchronization with the page before moving on to the next scr ...

Connecting AngularJS with select options

I am currently developing a checkout feature that calculates shipping prices based on the selected country. If the user picks United States, it should display US. For any other country selection, it should show Not US While the shipping function correctly ...

Encountering an error in Angular Chosen where the forEach function is not recognized

Implementing Angular Chosen for a multi-select dropdown menu to choose nationalities. https://github.com/localytics/angular-chosen An error message is popping up saying: "a.forEach is not a function" This error seems to occur regardless of whether one, ...

The text input is malfunctioning in the IE web browser

For my project, I am incorporating AngularJS but encountering an issue with the input type text in IE browsers specifically IE 11 and IE 10. The problem arises when clicking on the input box - while the focus is triggered, the cursor does not appear insid ...

Vue: Ensuring one method finishes executing before triggering the next one

I've implemented two methods in my Vue instance; const app = new Vue({ el: '#app', data: { id: null }, methods: { getId: function() { return axios.get('url') .then(response => response.data) .then(i ...

What is the best way to design a regular expression that will only match up to 12 numbers?

Is there a way to construct a regular expression that will validate a string containing up to 12 digits only? Thank you! ...

Add a unique CSS style to both text and image using anchor tags

Why isn't the hover effect of color transition and underline being applied to the image? It seems to only work for text. While I understand that the color transition may require a change in image color, shouldn't the underline still occur? This ...

What are the reasons behind the unexpected behavior of the replace() method in the newest Safari update?

What is causing the JS method replace() to behave incorrectly in Safari version 11.1 when using "$<" in the second argument? For example: "aaaXXXbbb".replace(/XXX/, '_before_$<_after_') The actual result is: "aaa$<_after_bbb" The ex ...

The functionality of Skrollr on mobile is currently not working properly due to the inability to prevent default actions

Encountering the following error on all chromium browsers while using the Skrollr library and accessing the website through mobile: "[Intervention] Unable to preventDefault inside passive event listener due to target being treated as passive. See < ...

Conceal or reveal input elements with a toggle function in jQuery by utilizing

When the user chooses an option from a dropdown list, I want to display or hide different input fields accordingly. I attempted to use toggle with boolean values, but it doesn't seem to be working as expected. I expect that if I send 'true' ...

How to perfectly position an image within a fixed full screen container

When you click on a thumbnail image, a full-screen overlay with a larger version of the image will be triggered using JavaScript. To ensure that the image is centered and resized inside the black overlay when the browser window size changes, I attempted t ...

Hapi/Node.js Reference Error for Request: Troubleshooting the Issue

I am facing an issue with my two API handlers: module.exports.loginWeb = function (request, reply, next) { if (request.auth.isAuthenticated) { return reply.redirect('/'); } if(request.method === 'get'){ rep ...

What could be the reason for my mongoose model failing to save in mongodb?

I am experiencing an issue with my simple Post model and route for creating posts. After submitting a new post using Postman, the request hangs for a moment before returning an error in JSON format. The model data is never saved successfully. Below is the ...

Using Vue.js causes an issue with Array.from(Object).forEach

When using vue.js 2 CLI, I typically define object data like this within the data() function: data(){ return{ user: { user_mail: '', user_password: '', user_confirm_password : '' ...