Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Ui-Router's ui-sref Just to Set Query Params

I have the following link.

<a ui-sref="...">Clicking this adds ?test=true to the query params.</a>

Is there a way to use the ui-sref attribute to just change query params without reloading the page or directing to another function?

like image 428
Code Whisperer Avatar asked Aug 29 '14 13:08

Code Whisperer


People also ask

What does UI sref do?

A ui-sref is a directive, and behaves similar to an html href . Instead of referencing a url like an href , it references a state. The ui-sref directive automatically builds a href attribute for you ( <a href=...> </a> ) based on your state's url.

What is the difference between ngRoute and UI-router?

The ui-router is effective for the larger application because it allows nested-views and multiple named-views, it helps to inherit pages from other sections. In the ngRoute have to change all the links manually that will be time-consuming for the larger applications, but smaller application nrRoute will perform faster.

What is UI-router?

UI-Router is the defacto standard for routing in AngularJS. Influenced by the core angular router $route and the Ember Router, UI-Router has become the standard choice for routing non-trivial apps in AngularJS (1. x).

How to add optional parameters to a query in UI router?

There are a number of ways optional parameters can be declared when using UI Router. So that query parameters are mapped to UI Router’s $stateParams object, you need to declare them in your state configuration’s URL template:

How to pass parameters using AngularJS UI-router?

Here are some methods of Passing Parameters using AngularJs UI-Router. Query Parameters are the parameters send through URL and visible to everyone. e.g. In Angularjs UI-Router you need to declare query parameters in your state configuration’s URL template. like this Unlike Query Parameters, Non-URL route parameters are hidden from the URL.

What is the difference between query parameters and non-URL parameters in angular?

Query Parameters are the parameters send through URL and visible to everyone. e.g. In Angularjs UI-Router you need to declare query parameters in your state configuration’s URL template. like this Unlike Query Parameters, Non-URL route parameters are hidden from the URL.

How do I set the default value for a Param parameter?

Settings available for params are described in the documentation of the $stateProvider value - {object|function=}: specifies the default value for this parameter. This implicitly sets this parameter as optional... array - {boolean=}: (default: false) If true, the param value will be treated as an array of values.


1 Answers

You should be able to do it by passing the arguments with the state.

try:-

<a ui-sref="yourStateName({test:true})">Clicking this adds ?test=true to the query params.</a>

Usage:

ui-sref='stateName' - Navigate to state, no params. 'stateName' can be any valid absolute or relative state, following the same syntax rules as $state.go()

ui-sref='stateName({param: value, param: value})' - Navigate to state, with params.

like image 148
PSL Avatar answered Oct 06 '22 17:10

PSL