AngularJS application design featuring a single page with various states

In the process of developing an AngularJS application using angular-ui-router, I encountered a situation where I have a page that lists items and allows filtering by name. The filtered results appear in the URL like /items?name=foo. Another page, such as users, allows navigation to the items page with filtering by user, for example, /items?userid=bar.

To handle this scenario, I created two states for the same page in the router configuration:

.state('index.items', {
   url: '/items?:name',
   controller: 'ItemCtrl as ctrl',
   templateUrl: 'item/index.html',
   brParent: 'index.home'
})
.state('index.itemsOfUser', {
   url: '/items?:userid&:name',
   controller: 'ItemCtrl as ctrl',
   templateUrl: 'item/index.html',
   brParent: 'index.home'
})

This setup ensures that clicking on the items page in the navbar directs to the normal items page without any filters applied.

However, I noticed that when navigating to the items page from the users section, the userid appears in the URL but $stateParams shows it as undefined. This behavior has left me puzzled as to why it is happening.

As a newcomer to Angular, I am unsure if my approach is correct or if there is a better way to achieve the desired functionality.

Answer №1

ui-router allows for the retrieval of query string parameters. Your code is very close, just a minor syntax correction:

.state('index.items', {
   url: '/items?name', // Remove the semicolon
   controller: 'ItemCtrl as ctrl',
   templateUrl: 'item/index.html',
   brParent: 'index.home'
})
.state('index.itemsOfUser', {
   url: '/items?userid&name', // Remove the semicolons
   controller: 'ItemCtrl as ctrl',
   templateUrl: 'item/index.html',
   brParent: 'index.home'
})

Refer to the documentation for more details: https://github.com/angular-ui/ui-router/wiki/URL-Routing#query-parameters

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

Issue with the statement not being recognized for counting the space bar

I created a counter but I'm having trouble incorporating if statements For example: if (hits == 1) {alert("hello world1")} if (hits == 2) {alert("hello world2")} if (hits == 3) {alert("hello world3")} if (hits == 4) {alert("hello world4")} This is t ...

The data stored in a FormGroup does not automatically transfer to FormData

I am currently facing an issue with uploading multi-part data to my API. To achieve this, I have created a FormData object for the upload process. Initially, I gather all the necessary user input data such as an image (file) and category (string). These va ...

Constantly showing false values in AngularJS Firebase array objects

Encountering an issue with retrieving data from Firebase. When viewing console.log($scope.statusbaca), it shows 2 queries, true and false. However, in the app it only displays false. Apologies for any language barriers as English is not my first language. ...

How to update a list in Angular after using a modal?

After implementing a modal for updating employee information in my ng-repeat employee list, I encountered an issue where the list is not updated upon clicking the save button - requiring me to refresh the page by pressing F5. I attempted to use $watch to ...

Transferring pictures between folders

I am currently developing an Angular app that involves working with two images: X.png and Y.png. My goal is to copy these images from the assets folder to a specific location on the C drive (c:\users\images) whose path is received as a variable. ...

Encountering 404 Error in controller's action methods following deployment on remote server

.JS Script $("#drpdwn").change(function () { var filter1 = document.getElementById("drpdwn").value; window.location = '/Controller/GetByFilter?filter=' + filter1; }); Action Method in Controller: public ActionResult GetByFilter(string ...

Switch between various components using multiple buttons in Next.js

I am in the process of creating a component that will display different components depending on which button the user selects. Here are the available “pages”: ` const navigation = [ { name: "EOQ", return: "<EOQgraph />" }, { nam ...

What is the best way to open a page in a new tab within the row command event of a gridview?

Here is the code snippet I am working with: protected void gv_inbox_RowCommand(object sender, GridViewCommandEventArgs e) { int index = Convert.ToInt32(e.CommandArgument); if (e.CommandName == "sign") { Session["TransYear"] = int.Pars ...

Axios - Show the error message returned from validation as [object Object]

Within my API, I include the following validation logic: $request->validate([ 'firstname' => 'required', 'lastname' => 'required', 'username' => 'required|unique:us ...

Put identical objects into several arrays at the same time

This question is in relation to a previous query about arranging 3 arrays to correspond with each other. After creating my objects, I am now attempting to push values into their respective arrays based on JSON data received. let objName = ["object1", "obj ...

What is the best way to incorporate inline css in Angular for various views that share identical content but require distinctive styles?

Below is my HTML code: <p ng-if= "editMode=='edit'" style="margin-left: 1px;margin-top: -2px;">===some messages---</p> <p ng-if= "editMode=='addNew'" style="margin-left: 1px;margin-top: -2px;">===some messages- ...

How can you use JavaScript to create a JSON object using values from an HTML form textarea and then send

I need to create an HTML form that will send specific information in the Http-request body. { “info” : { “id” : “123” “text1” : <data from html text-box> } Therefore, my goal is to convert the provided data into a JavaScri ...

Is there a valid substitute for using hot links with images?

Currently, I am instructing a group of individuals on the intricacies of web development with a specific focus on AngularJS. As part of an exercise that I designed for them, they are required to fetch data from the freely accessible pokeapi.co. One interes ...

Transfer multiple files by dragging and dropping them into a single file upload option

I was experimenting with adding a drag and drop feature to my website. After trying several scripts, I came across one on CodePen. However, the script allows for the upload of multiple files, while I only need to upload a single PDF or .doc file. Can someo ...

HTML Quiz Form Data Submission via Email

I recently designed a quiz using HTML that consists of 5 questions. I have programmed it in a way that only one question appears at a time, keeping the upcoming questions hidden from users taking the quiz. However, since each question is within a separate ...

Stay up-to-date with our constantly updating news feed that brings fresh content to you on

My challenge is to automatically refresh a news feed by adding 15 new posts every 5 minutes. Currently, I have a countdown timer displayed within a paragraph tag on my HTML page. When this timer hits zero, I want the feed to be updated with fresh content. ...

Guide on changing input field values based on selected radio button combinations using both AND and OR selection with JQuery

Here's the issue at hand: I have 6 sets of radio buttons, each representing a binary "yes/no" statement. In addition, there are 4 read-only input fields where I need to assign either a value of 1 or 0 based on the user's selection of radio butto ...

Ways to remove identical key from distinct object within an array

Is there a way to remove specific date keys from multiple objects in an array of objects? I attempted to use a for loop and the delete function, but it doesn't seem to be working. var data = matchScoreData.tbl_FallOfWicket; matchScoreData.tbl_Fall ...

What is the most effective approach for managing nested callbacks in Node.js/Expressjs?

My server side coding is done using Node.js and Expressjs, with MongoDB as the backend. Although I am new to these technologies, I need to perform a list of actions based on requests. For example, in user management: Check if the user is already registe ...

Successive vows

I'm trying to retrieve responses from four promises, but I currently have to call each function in sequence one after the other. In my code, you can see that I trigger the next function within the promise callback of the previously called function. H ...