UI-Router: Change part of the url - angularjs

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.

Related

In angular js- how to redirect to home page , when user changed url

In angular js , how to redirct the url .
When user changed tab1 to tab2 in url, it must be redirect to tab1.
There are many ways to handle it.. Few of them:
Manage in your Routeconfig.
Manage it in your controller, by changing $location.path()
Manage via $locationChangeStart event listener. Like below :
scope.$on('$locationChangeStart', function(event, next, current) {
// manage navigation here.
});
See this attached Fiddle example, try with uncommenting code as well.

$routeParams showing Url variable parameter

I am trying to use routeProvider to create a search page. Below I have something like so:
}).when("/search/:value", {
templateUrl: "app/views/search/search.html"
What I want to show in the url is not
/search/searchValue
but more like
/search/value=searchValue
I am setting the location as so:
$location.path('/search/').search({ value: $scope.filterValue }); where $scope.filterValue is the searchValue.
When I use this, I'm not able to view my page due to the :value. How can i change the url to the one I want as in you have the routeParam show in the url link?
Thanks,
All I had to do was use /search/?. The value after the "?" was the parameter.

I have created one button. I want to redirect to another page by clicking on it in backbone. How can i do that?

I have created one button. I want to redirect to another page by clicking on it in backbone. How can i do that?
I tried following code but it does not work:
router.navigate("/file:///code/order/table.html",true);
Using JavaScript:
window.location.href = 'file:///code/order/table.html';
But I would suggest to use relative path and simple anchor tag:
Table
the backbone router usage is wrong here. from the doc router.navigate(fragment, [options]) this is the function usage, the 1st argument should be fragment, which is registered via the router.routes.
if you would like to navigate to the other file, you may consider the window.location.href

Disable ui-router to some url

I have some urls, like /some_url_1/, /some_url_2/ and /static_url/.
In my app-config i defined ui-router states for urls /some_url_1/ and /some_url_2/, but url /static_url/ must doing full reaload page as if i dont using angular.
Now, when on page /some_url_1/ i click to a[href="/static_url/"] ui-router try to find state for this url, but cant find and page content stay as at /some_url_1/.
How i can disable /static_url/ for ui-router and make full reload page to
You can use this (It's not tested):
Outside link
Use target="_self"
<a href="url/to/reload.html" target="_self">link</link>

Updating URL in Angular JS without re-rendering view

I'm building a dashboard system in AngularJS and I'm running into an issue with setting the url via $location.path
In our dashboard, we have a bunch of widgets. Each shows a larger maximized view when you click on it. We are trying to setup deep linking to allow users to link to a dashboard with a widget maximized.
Currently, we have 2 routes that look like /dashboard/:dashboardId and /dashboard/:dashboardId/:maximizedWidgetId
When a user maximizes a widget, we update the url using $location.path, but this is causing the view to re-render. Since we have all of the data, we don't want to reload the whole view, we just want to update the URL. Is there a way to set the url without causing the view to re-render?
HTML5Mode is set to true.
In fact, a view will be rendered everytime you change a url. Thats how $routeProvider works in Angular but you can pass maximizeWidgetId as a querystring which does not re-render a view.
App.config(function($routeProvider) {
$routeProvider.when('/dashboard/:dashboardId', {reloadOnSearch: false});
});
When you click a widget to maximize:
Maximum This Widget
or
$location.search('maximizeWidgetId', 1);
The URL in addressbar would change to http://app.com/dashboard/1?maximizeWidgetId=1
You can even watch when search changes in the URL (from one widget to another)
$scope.$on('$routeUpdate', function(scope, next, current) {
// Minimize the current widget and maximize the new one
});
You can set the reloadOnSearch property of $routeProvider to false.
Possible duplicate question : Can you change a path without reloading the controller in AngularJS?
Regards
For those who need change full path() without controllers reload
Here is plugin: https://github.com/anglibs/angular-location-update
Usage:
$location.update_path('/notes/1');
I realize this is an old question, but since it took me a good day and a half to find the answer, so here goes.
You do not need to convert your path into query strings if you use angular-ui-router.
Currently, due to what may be considered as a bug, setting reloadOnSearch: false on a state will result in being able to change the route without reloading the view. The GitHub user lmessinger was even kind enough to provide a demo of it. You can find the link from his comment linked above.
Basically all you need to do is:
Use ui-router instead of ngRoute
In your states, declare the ones you wish with reloadOnSearch: false
In my app, I have an category listing view, from which you can get to another category using a state like this:
$stateProvider.state('articles.list', {
url: '{categorySlug}',
templateUrl: 'partials/article-list.html',
controller: 'ArticleListCtrl',
reloadOnSearch: false
});
That's it. Hope this helps!
We're using Angular UI Router instead of built-in routes for a similar scenario. It doesn't seem to re-instantiate the controller and re-render the entire view.
How I've implemented it:
(my solution mostly for cases when you need to change whole route, not sub-parts)
I have page with menu (menuPage) and data should not be cleaned on navigation (there is a lot of inputs on each page and user will be very very unhappy if data will disappear accidentally).
turn off $routeProvider
in mainPage controller add two divs with custom directive attribute - each directive contains only 'templateUrl' and 'scope: true'
<div ng-show="tab=='tab_name'" data-tab_name-page></div>
mainPage controller contains lines to simulate routing:
if (!$scope.tab && $location.path()) {
$scope.tab = $location.path().substr(1);
}
$scope.setTab = function(tab) {
$scope.tab = tab;
$location.path('/'+tab);
};
That's all. Little bit ugly to have separate directive for each page, but usage of dynamic templateUrl (as function) in directive provokes re-rendering of page (and loosing data of inputs).
If I understood your question right, you want to,
Maximize the widget when the user is on /dashboard/:dashboardId and he maximizes the widget.
You want the user to have the ability to come back to /dashboard/:dashboardId/:maximizedWidgetId and still see the widget maximized.
You can configure only the first route in the routerConfig and use RouteParams to identify if the maximized widget is passed in the params in the controller of this configured route and maximize the one passed as the param. If the user is maximizing it the first time, share the url to this maximized view with the maximizedWidgetId on the UI.
As long as you use $location(which is just a wrapper over native location object) to update the path it will refresh the view.
I have an idea to use
window.history.replaceState('Object', 'Title', '/new-url');
If you do this and a digest cycle happens it will completely mangle things up. However if you set it back to the correct url that angular expects it's ok. So in theory you could store the correct url that angular expects and reset it just before you know a digest fires.
I've not tested this though.
Below code will let you change url without redirection such as: http://localhost/#/691?foo?bar?blabla
for(var i=0;i<=1000;i++) $routeProvider.when('/'+i, {templateUrl: "tabPages/"+i+".html",reloadOnSearch: false});
But when you change to http://localhost/#/692, you will be redirected.

Resources