"Modifying the query string in AngularJS: A step-by-step guide

For instance, let's say I have the following URL: http://local.com/. When I invoke a function in my SearchController, I want to set the parameter text=searchtext and obtain a URL like this:

http://local.com/?text=searchtext
.

What is the correct way to achieve this? I attempted using

$location.search('text', 'value');
but ended up with the following URL:

http://local.com/#?text=searchtext

$scope.searchTracks = function() {

        Search.search.get($scope.params, function(data) {
            /** Set params in query string */
            $location.search('text', $scope.text);
            $location.search('sorted', $scope.sorted);

        });

    }

Answer №1

Make sure to include the specific path:

$location
  .path('/specified/path/for/new/link')
  .search({
    'text': $scope.text,
    'sorted': $scope.sorted
  });

The resulting URL will look like this:

http://localhost/specified/path/for/new/link?text={{$scope.text}}&sorted={{$scope.sorted}}

Alternatively, you can manually assign them this way:

$location.url('/specified/path/for/new/link?text' + $scope.text + '&sorted=' + $scope.sorted);

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

Node.js returns a 404 Not Found error when trying to access localhost

After creating a basic login system using Node.js and Express 4.x, I encountered an issue where accessing localhost:3000 resulted in a "Not Found 404" error. Upon investigation, I realized that the problem stemmed from discrepancies between code written fo ...

a pair of browser windows displaying a web application built with ASP.NET MVC

We are currently in the process of developing an ASP.NET MVC web application for internal use within our organization. Our users' productivity can greatly benefit from having a larger screen space, which is why we have decided to provide each user wit ...

window.onresize = function() { // code here

Here's an example of code I've been working on: $(document).ready(function (e) { adjustSize(); $(window).resize(adjustSize); function adjustSize() { var windowWidth = parseInt($(window).width()); if (windowWidth > ...

Why does a string expression not function properly with the ng-bind directive, but a number expression does?

Take a look at these AngularJS examples: Example 1: <!DOCTYPE html> <html> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> <body> <div ng-app="" ng-init="quantit ...

Determine the width of two inline input fields

Showing two inputs side by side: +------------+ +--------------------------+ | ID='inputA'| | ID='inputB' | +------------+ +--------------------------+ +------------------------------------------+ A ...

Using session variables to store connection objects for database rollback

My web application is divided into multiple modules spread across different JSP pages. Currently, I am facing the challenge of using separate oracle connection objects on each page due to scope limitations. The problem arises when I need to rollback databa ...

An issue has occurred in AngularJS where the error message "ng areq not

I'm facing an issue with my meta controller, as I am trying to alter the meta tags dynamically. When checking the console, I encounter the error message error ng areq not a function. I have looked on StackOverflow for similar issues but couldn't ...

Encountering a 500 server error while attempting to retrieve content from Google Images through the Web Speech API

My current project involves utilizing the Web Speech API to dynamically gather free images from Google. Here's how it works: I extract the search keyword using the Web Speech API in JavaScript. The keyword is then sent to the server (PHP) via an a ...

Vue.js: Attaching a function to a Template

I am struggling to find a way to bind my screen height to a calculated value in my code. Unfortunately, the current implementation is not working as expected. I would greatly appreciate any guidance on how to resolve this issue. <template> <b ...

What is the best way to encode an image into JSON format?

let canvas = document.createElement('canvas'); let context = canvas.getContext( '2d' ); context.drawImage( video, 0, 0 ); let image_src = canvas.toDataURL('image/jpeg'); let dataURL = canvas.toDataURL("image/jpeg"); let image= ...

Arranging method for organizing a set of items into order

I want to arrange the elements in a list using JavaScript/jQuery. The initial sorting works fine, but I also need it to revert back to the unsorted view when clicked again. This cycle should repeat continuously. Check out the demo here: Sorting Demo ...

Performing a sequence of actions using Jquery Queue() function and iterating through each

I am facing an interesting challenge with an array called result[i]. My goal is to iterate through each field in the array and add it to a specific element on my webpage. $("tr:first").after(result[i]); However, I would like this process to happen with a ...

UnhandledPromiseRejectionWarning when chaining functions in Node Express is a common issue that needs attention

Currently, I am refactoring a Node Endpoint to handle two tasks: Verify if the user exists Add the user to the database controller.js exports.signup = (req, res) => { methods.checkUser('email', req.body.email) .then(methods.addUser(r ...

The res.redirect function is not functioning as anticipated

I recently added a middleware to my express app that redirects users to logout if the token is invalid. export async function validateAuthTokenMiddleware( req: Request, res: Response, next: NextFunction, ): Promise<NextFunction | void> { ...

Dealing with asynchronous requests in server-side node applications

Currently, I am in the process of constructing a basic node service that carries out the following functionalities: Handles incoming GET requests from web clients Parses the parameters provided Utilizes these parameters to asynchronously query another RE ...

What are the steps to launch an Angular.js application?

I have a question about deploying my Angular.js app - I tried using Heroku but encountered an error. Any suggestions on the best platform to use? UPDATE I'm attempting to deploy my Angular.js app on Heroku as a Node.js application, but it's no ...

What is the simplest way to run a basic express js script?

I am encountering an issue while trying to run a basic express script in JavaScript. Every time I attempt to execute the script, I keep getting an error message stating that "require is not defined". Below are snippets of the code. Thank you! const expres ...

Utilize jQuery to extract all values from the second cell of a table and store them in an array

I have a task that involves pushing numbers from text boxes into an array. These text boxes are located in the second cell of each table row, while the third cell contains a date picker. Currently, the code snippet provided retrieves all values from both ...

Basic jQuery Element Selection

1) Is there a way to trigger an alert only when images like 1_1.jpg, 1_2.jpg, 1_3.jpg or 2_1.jpg, 2_2.jpg, 2_3.jpg are selected and none of the others? (similar to *_1.jpg, *_2.jpg, *_3.jpg) 2) How can I shuffle the order of the image positions randomly ( ...

Trouble arises when adding HTML elements to a Content Editable Div. Any text inputted after programmatically inserting HTML content will merge with the last HTML tag instead

https://i.sstatic.net/bKIVm.pngI am currently working on a project that involves creating message templates within an app. Users have the ability to add placeholders for fields like names to these templates by clicking a button. They can also remove these ...