$location.search not working if search parameters not changed - angularjs

$location.search is not reloading route if search parameter are same as parameter in current location.
if window.location is "url#RouteName?param1=value1".
And in some button click calling method which has following code.
$location.path("/RouteName").search({param1 :"value1"});
Then Route is not getting reloaded, how to force reload. I have set reloadOnSeach : true in route config.
$routeProvider.when("RouteName", {templateurl:"pag1.html",controller:"MyCtrl",reloadOnSearch:truee});

reloading the route is not done by $location.search explicitly, in fact it doesn't do anything if the url has not changed by the function call
use
$route.reload()

Reload will realod the route.
In case you want to change the search:
$location.search({'slide_id': slide_id})
If your route params do not change (or change to the same value) then you might need to force the location change:
$scope.$emit('$locationChangeSuccess');

Use window.location.search instead of location.search. It will work.

Related

Location search parameters disappear when I load a new template

I am using $location.search(params) to store an array in my url. This array has determines how the page will load. It works well only problem is that I have a set of tabs on my page, when ever I click the the tab a new template is loaded with a new controller and the url variables disappear.
I dont understand this behavior $location.search() is not being called again in the new controller. How can I get the Url to stay static?
In the $routeProvider set reloadOnSearch: false so you don't reload when $location.search() changes.
https://docs.angularjs.org/api/ngRoute/provider/$routeProvider
Change the path using
$location.path('/differentRoute').search(params);
Doing so would change the route and set the search parameters again.

UI-Router: Change part of the url

I'm using Angular's UI-router. All my routes looks like that:
/:name/....
for example /:name/test/:id, /:name/logs, /:name/list/:id
I want to change the name part when I click on a button, and reload the page. How can i do that globally without knowing exactly what route I'm in ?
$state.go($state.current, { name, 'newValue' });
Or something similar with ui-sref.

change $location search preventing push to history

I have a router view with reloadOnSearch: false, it is a search page.
When entering without any search parameter I attempt to set some defaults:
entering in: #/search
I set defaults pars: #/search?currpage=1&pagesize=20
using $location.search({currpage:1,pagesize:20}) the job is done, but I also get the new url pushed in the history, which is as expected, but in this special case it breaks the logic of the navigation flow (clicking back button gets to #/search and again sets defaults and pushes)
Is there a way to replace the search string preventing the history push?
Have you tried to you use the dedicated replace() method ? It prevent to add an history record :
$location.search({currpage:1,pagesize:20}).replace()
Never used it after a $location.search() but it definitely works on $location.path()

Angular location service is not changing path

I am trying to execute $location.path(path) from within my code but when I check in my browser I see that the url changes for a millisecond or so and comes back to the previous url, hence my page doesn't change.
Any idea?
FWIW, I also had that problem with an anchor defined like this:
book today!
It turns out that the hash (#) in the href attribute caused the app to loop back to the default route all the time.
I changed the link by removing the hash, and then it worked:
book today!
$location.path() is a getter function to set you need to call $location.path('/yourpath')

AngularJS - How can I do a redirect with a full page load?

I want to do a redirect that does a full page reload so that the cookies from my web server are refreshed when the page loads. window.location = "/#/Next" and window.location.href = "/#/Next" don't work, they do an Angular route which does not hit the server.
What is the correct way to make a full server request within an Angular controller?
For <a> tags:
You need to stick target="_self" on your <a> tag
There are three cases where AngularJS will perform a full page reload:
Links that contain target element
Example: link
Absolute links that go to a different domain
Example: link
Links starting with '/' that lead to a different base path when base is defined
Example: link
Using javascript:
The $location service allows you to change only the URL; it does not allow you to reload the page. When you need to change the URL and reload the page or navigate to a different page, please use a lower level API: $window.location.href.
See:
https://docs.angularjs.org/guide/$location
https://docs.angularjs.org/api/ng/service/$location
We had the same issue, working from JS code (i.e. not from HTML anchor). This is how we solved that:
If needed, virtually alter current URL through $location service. This might be useful if your destination is just a variation on the current URL, so that you can take advantage of $location helper methods. E.g. we ran $location.search(..., ...) to just change value of a querystring paramater.
Build up the new destination URL, using current $location.url() if needed. In order to work, this new one had to include everything after schema, domain and port. So e.g. if you want to move to:
http://yourdomain.example/YourAppFolder/YourAngularApp/#/YourArea/YourAction?culture=en
then you should set URL as in:
var destinationUrl = '/YourAppFolder/YourAngularApp/#/YourArea/YourAction?culture=en';
(with the leading '/' as well).
Assign new destination URL at low-level: $window.location.href = destinationUrl;
Force reload, still at low-level: $window.location.reload();
After searching and giving hit and trial session I am able to solove it by first specifying url like
$window.location.href = '/#/home/stats';
then reload
$window.location.reload();
I had the same issue. When I use window.location, $window.location or even <a href="..." target="_self"> the route does not refresh the page. So the cached services are used which is not what I want in my app. I resolved it by adding window.location.reload() after window.location to force the page to reload after routing. This method seems to load the page twice though. Might be a dirty trick, but it does the work. This is how I have it now:
$scope.openPage = function (pageName) {
window.location = '#/html/pages/' + pageName;
window.location.reload();
};
Try this
$window.location.href="#page-name";
$window.location.reload();

Resources