Working with URL Route Parameters in AngularJS using the ui-router

I am trying to find a way to refresh only the nested view of my application and add a route parameter in order to enable URL routing. However, I am struggling to achieve this. Initially, I had it working using a query like the following:

$location.search('userId', user._id);
//http://localhost:9000/#/user/?userId=123456789

The URL format that I am aiming for is as follows, with the userId = 123456789:

http://localhost:9000/#/user/123456789

This is an excerpt from my app.js file:

$stateProvider
    .state('index', {
        url: '/',
        views: {
            '@' : {
                templateUrl: 'views/layout.html'
            },
            'top@index' : {
                templateUrl: 'views/top.html',
                controller: function($scope, $state) {
                    $scope.userLogOut = function() {
                        $state.go('login');
                    };
                }
            },
            'left@index' : { templateUrl: 'views/left.html' },
            'main@index' : { templateUrl: 'views/main.html' }
        }
    })
    .state('index.user', {
        url: 'user:userId',
        templateUrl: 'views/user/user.html',
        controller: 'UserCtrl'
    })
    .state('index.user.detail', {
        url: '/',
        views: {
            'detail@index' : {
                templateUrl: 'views/user/details.html',
                controller: 'DetailCtrl'
            }
        }
    })

Within my controller:

$state.reload('index.user.detail', {userId: $scope.user._id});

Answer №1

When utilizing ui-router, you can navigate using

$state.go('index.user', { userId: {{yourid}} });

To enable query string parameters with ui-router, structure your state as follows:

.state('index.user', {
    url: 'user/?userId=:param',
    templateUrl: 'views/user/user.html',
    controller: 'UserCtrl'
})

This configuration will make this URL functional:

//http://localhost:9000/#/user/?userId=123456789

If you prefer a URL without query string parameters, it would look like this:

.state('index.user', {
    url: 'user/:userId',
    templateUrl: 'views/user/user.html',
    controller: 'UserCtrl'
})

http://localhost:9000/#/user/123456789

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

flexible style class in css

Currently, I am facing an issue where I need to display a variable number of items with a uniform margin setting that changes across the set. In simple terms, if I have a set like [1,2,3,4,5], it would look like this: 1 2 3 4 ...

Angular6 - accessing elements that are not visible on the page

Currently, I am facing a situation where I have a component embedded in my HTML template that needs to remain hidden until a certain condition is met by checking a box. The tricky part is that the condition for setting "showControl" to true relies on calli ...

The method for transforming all headers into permalinks with jquery/javascript

I am looking for a way to transform all headers on a page into clickable permalinks using jQuery or JavaScript. Here is the HTML code: $('h3').each(function() { var id = $(this).attr('id'); if (id) { //Check if the element has a ...

Utilizing Next.JS and Nodemailer to seamlessly send emails through a contact form

I'm facing an issue with my contact form in next.js. Everything was working fine until I deployed it on Vercel. Although I don't see any errors, I am not receiving emails on my Gmail account after submitting the form. Additionally, I am not getti ...

Error: Undefined variable in JavaScript obfuscation

My website has several small JavaScript files that I currently merge into one file and then minimize using terser. The website functions perfectly with the minimized version. Now, I want to take it a step further and obfuscate the code using JavaScript-Ob ...

Displaying ember component in a separate template from its parent

Consider this scenario: I have an absolutely positioned sidebar containing additional absolute elements. Within the sidebar, there is a button that triggers a menu to appear: <button>Click me</button> {{#if shouldDisplayMenu}} {{view App.M ...

Issues with fundamental JavaScript client-side code

As a newcomer to the world of javascript and jQuery, I am diving into my first experiment with javascript. My initial focus has been on changing questions by clicking next or previous buttons. The goal is to create a dynamic quiz webpage that updates quest ...

What is the best method for displaying the date/time with timezone in AngularJS?

While utilizing the AngularJS filter date, I have incorporated the following code snippet: var date = new Date("09 apr 2015 12:00:50 UT"); $filter('date')(date, "dd MMM yyyy hh:mm:ss a Z"); The current output is: 09 Apr 2015 08:19:04 PM + ...

Marshaling a payload in Groovy at a snail's pace

I am currently working on an Angular/Groovy Application and facing slow performance while marshaling a payload in the backend (Groovy). The process takes a significant amount of time to complete, send the data to the frontend, and render it. In the backen ...

React with debouncing and parameters

Currently, I am attempting to implement debouncing for a function using underscore's debounce, which is passed as a prop to a component. In the past, I was successful in achieving this by using the following approach: componentWillMount() { this. ...

Error on Laravel 7: Unable to retrieve resource - the server returned a 400 (Bad Request) status code

Seeking assistance with a technical issue involving Razorpay. I'm encountering difficulty passing a value from the controller to JavaScript in the view. The data-order_id field is not populating, resulting in the error mentioned above, indicating a nu ...

There was an issue retrieving the metadata for the bootstrap package using the git+ssh protocol

While attempting to install angular devkit and other dependencies using npm install, I encountered the following error message: Could not retrieve metadata for bootstrap@git+ssh://example@example.com/gios/Web-Standards-Bootstrap.git#05a343dddce91dd157702 ...

What is the process for adding an item to an object?

I currently have the following data in my state: fbPages:{'123':'Teste','142':'Teste2'} However, I am in need of a dynamic solution like the one below: async getFbPages(){ var fbPages = {} awa ...

Angular's change detection mechanism triggers too frequently

I'm dealing with a situation in one of my controllers where I have the following code: @HostListener('window:resize') onResize() { this.currentWindowWidth = window.innerWidth; } This code determines different views to render based on the ...

"Pressing the 'back' button on your browser takes

Is there a way to navigate back to the main page by clicking on an image? After selecting First, Picture1 will be shown. How can I go back to the selection page for further choices? <a href="picture1.jpg"> <h3>First</h3></a> <a ...

What is the process for downloading a file from a webapi on Internet Explorer with an angular post request?

I have encountered an issue with my code while using angularjs http post to download a file from the Web Api. The code works perfectly fine in Google Chrome and Firefox, however it fails in Internet Explorer. Here is the code snippet: $scope.CallApi = fun ...

Guide to making a simple bookmarklet that replicates the functionality of Selenium

I have experience with Selenium, Java/Python, XPaths and I am in search of a simple Javascript bookmarklet that acts as a basic framework for performing similar operations as Selenium on the current webpage. For example, (in pseudocode), begin document.cli ...

How can I style the options and optgroups of a select dropdown with CSS, focusing on padding and margins?

In my experience, I have found that padding or margin CSS properties only seem to affect the appearance of options within the select menu in Firefox browser. I attempted the same styling in Internet Explorer and Chrome but it did not have any effect. Is ...

I keep running into errors when trying to call functions within a mapped array in my React JS code

During my code execution, I am invoking a function by iterating through an array let artistId = artist._id; orderData.products.map((cartproduct, index) => { const sendOrderData = { product: cartproduct._id, qty: cartproduct.qty, }; ...

Is there an issue with Ajax connecting to the database?

I have created an HTML file where I need AJAX to connect to the database in the background and fetch the selected city based on the user's pre-selected country. Essentially, I want the database to load all the cities in the drop-down menu automaticall ...