Issues with Angular routing not accepting request parameters

As I attempt to navigate to a partial template upon form submission in Angular using ng-view, I encounter an issue with passing parameters through the URL. Specifically, I am trying to pass a parameter ?username=Joe and retrieve it in the new partial using $routeParams.

The routing functions properly when no parameters are passed: $location.path('/submit'); However, if I try to include parameters, the form button does not function as expected:

$location.path('/submit?userName=joe');

Here is my Angular code:

index.html:

<body>
<div ng-view></div>
</body>

loginPartial.html:

<form ng-submit="submit()">
    <button type="submit">Submit</button>
</form>

messagePartial.html:

<p>Hello {{userName}}</p>

Angular code:

angular.module('app',['ngRoute'])
.config(['$routeProvider',
function($routeProvider){
    $routeProvider
        .when('/',{
            templateUrl:'loginPartial.html',
            controller:'LoginController'
        })
        .when('/submit',{
            templateUrl:'messagePartial.html',
            controller:'MessageController'
        })
        .otherwise({
            redirectTo:'/'
        });

}])
.controller('LoginController',['$scope','$location',
function($scope,$location){
    $scope.submit=function(){
    $location.path('/submit');//routing works with no parameters passed
    //$location.path('/submit?userName=joe');//routing fails if parameters passed
    };
}])
.controller('MessageController',['$scope','$routeParams',
function($scope,$routeParams){
    $scope.userName=$routeParams.userName;
}]);

Answer №1

To achieve this, utilize

$location.path('/submit').search({userName:'Joe'})

In addition, if you encounter difficulties with $routeParams, consider injecting $route and including the following code in your controller:

var _params = $route.current.params

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 filtering (ngRepeat) in a table across multiple columns is not fully functional

I am looking to apply a filter on my table that is created using ng-repeat. <tbody> <tr ng-repeat="x in contact.listeContacts | filter:contact.searchText track by $index"> <td> &l ...

Database not receiving input data from AngularJS form submission

Having some trouble saving form data to the database using angularjs. Not sure what I'm doing wrong. Here's my HTML form: <form id="challenge_form" class="row" > <input type="text" placeholder="Challenge Name" ng-model="ch ...

Retrieve the child grid's data source in Kendo using Angular

I am currently working with a master detail grid in Kendo Grid and using the Angular version of the Kendo Grid. One interesting feature I have added is a custom button for adding a new record in each row of the grid. When this button is clicked, a new rec ...

Utilizing Filters (Pipes) in Angular 2 Components without Involving the DOM Filters

When working in Angular 1, we have the ability to use filters in both the DOM and Typescript/Javascript. However, when using Angular 2, we utilize pipes for similar functionality, but these pipes can only be accessed within the DOM. Is there a different ap ...

Issue: Unable to use the b.replace function as it is not recognized (first time encountering this error)

I can't seem to figure out what I'm doing wrong. It feels like such a silly mistake, and I was hoping someone could lend a hand in solving it. Here is my controller - I'm utilizing ng-file-upload, where Upload is sourced from: .controller( ...

The framework of AngularJS modules

As I delve into structuring multiple modules for our company's application, I find myself at a crossroads trying to design the most optimal architecture. Research suggests two prevalent approaches - one module per page or a holistic 'master' ...

Prevent tab switching from occurring by disabling click functionality on tabs

I've got different tabs set up in a similar way <md-tabs md-selected="selectedTab()"> <md-tab label="General"></md-tab> <md-tab label="Type"></md-tab> <md-tab label="Details"></md-tab> </md-tab ...

Best event for Angular directive to bind on image onload

I'm currently working on incorporating this particular jQuery plugin into my Ionic application. Although I have a good grasp of creating an Angular directive to encapsulate the plugin, I am encountering difficulties in successfully implementing it. B ...

Display a singular value using ng-show when identical data is received for the specified condition

This section will display the result based on the selected language. For example, if "English" is selected in myAngApp1.value, it will display the content in English. However, since all four languages have an English value in SharePoint list, it displays t ...

Two distinct controllers: concealing elements through the operation of one of them

I am facing a challenge with my HTML elements: In one form-group, I have the following input: <div class="form-group"> <input type="search" ng-model="search"/> </div> This particular form-group needs to be hidden when another form-g ...

Error encountered while rendering ngMessages error in AngularJS due to ngif markup issue

I am currently utilizing ngMessages to validate a dynamically generated form using ngRepeat. While ngMessages is functioning as expected, I want the validation error messages to only display when the submit button is clicked. To accomplish this, I am emplo ...

Angular.js enables seamless synchronization between contenteditable elements and the $scope object by automatically updating the

I'm completely new to Angular.js and have been exploring various tutorials to grasp the concept of two-way binding with contenteditable elements within an ng-repeat. Currently, I am utilizing a shared 'store' between controllers like this: ...

Tips for refreshing an angularjs $scope using $.get jquery

Attempting to implement the code below using $scope: var scopes = "https://www.googleapis.com/auth/contacts.readonly"; setTimeout(authorize(), 20); function authorize() { gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, h ...

HTML5 on its own versus the dynamic duo of jQuery Mobile and AngularJS

We are in the process of creating a versatile mobile app that will work on multiple platforms using HTML5, JavaScript, and CSS. Our design approach includes utilizing Bootstrap for a responsive user interface, and Cordova to package the application for bot ...

User class instantiation in livequery is initiated

Is it possible to initialize the user class in a live query? I have initialized the user class in my index.js and it shows up in my network inspector. However, when I attempt to query, nothing appears in the websocket. Below is the code showing how I init ...

Include an object in the ng-map marker parameter

How to utilize markers in ng-map with AngularJS <ng-map zoom-to-include-markers="auto" default-style="false" class="myMap"> <marker ng-repeat="Customer in ListForDisplay" position="{{Customer.location.lat}},{{Customer.location.lng}}" icon ...

Start with Angular routing, then switch to mvc routing if needed

My project utilizes Angular and node.js on IIS 7.5 for the frontend, and a .NET Web API for backend calls. The Angular routing and API routing are working smoothly when testing with Visual Studio/IIS Express and ng serve. However, they run on separate por ...

Is there a way to automatically cancel any pending $http requests when the route changes in AngularJS?

If we want to cancel the $timeout and $interval when the route changes, we can use '$destroy'. But what about aborting any pending $http requests similar to $interval and $timeout? ...

A guide to printing a web page within an ion-view container

I am currently utilizing the Ionic Framework for my web application. Each page within the app is displayed using ion-view. I have a requirement to display graphs and possibly save them as PDF files. Below is a snippet of my code: <ion-view title="Repo ...

The variable _spPageContextInfo has not been defined, resulting in a ReferenceError

The code in my JavaScript file looks like this: var configNews = { url:_spPageContextInfo.webAbsoluteUrl, newsLibrary: 'DEMONews', listId: '' }; // Attempting to retrieve the List ID $.ajax({ url: configNews.url + "/_a ...