How to delete ‘#’ sign in angular-ui-router URLs

You need to enable HTML5Mode if you want navigation without hash tags: app.config([“$locationProvider”, function($locationProvider) { $locationProvider.html5Mode(true); }]); You will also need to tell angular the root URL of your app by adding the following code to the <head> of your HTML file: <base href=”https://stackoverflow.com/”> Be aware that support for HTML5 mode depends on the browser. … Read more

Angular ui router unit testing (states to urls)

Been having this issue as well, and finally figured out how to do it. Here is a sample state: angular.module(‘myApp’, [‘ui.router’]) .config([‘$stateProvider’, function($stateProvider) { $stateProvider.state(‘myState’, { url: ‘/state/:id’, templateUrl: ‘template.html’, controller: ‘MyCtrl’, resolve: { data: [‘myService’, function(service) { return service.findAll(); }] } }); }]); The unit test below will cover testing the URL w/ params, … Read more

Dynamically set the value of ui-sref Angularjs

Looks like this is possible to do after all. A breadcrumb on GitHub by one of the ui-router authors led me to try the following: <a ng-href=”https://stackoverflow.com/questions/24349731/{{getLinkUrl()}}”>Dynamic Link</a> Then, in your controller: $scope.getLinkUrl = function(){ return $state.href(‘state-name’, {someParam: $scope.someValue}); }; Turns out, this works like a charm w/ changing scoped values and all. You can … Read more

Angular-ui-router: ui-sref-active and nested states

Instead of this- <li ui-sref-active=”active”> <a ui-sref=”posts.details”>Posts</a> </li> You can do this- <li ng-class=”{active: $state.includes(‘posts’)}”> <a ui-sref=”posts.details”>Posts</a> </li> Currently it doesn’t work. There is a discussion going on here (https://github.com/angular-ui/ui-router/pull/927) And, it will be added soon. UPDATE: For this to work, $state should be available in view. angular.module(‘xyz’).controller(‘AbcController’, [‘$scope’, ‘$state’, function($scope, $state) { $scope.$state = … Read more

How do I get the Back Button to work with an AngularJS ui-router state machine?

Note The answers that suggest using variations of $window.history.back() have all missed a crucial part of the question: How to restore the application’s state to the correct state-location as the history jumps (back/forward/refresh). With that in mind; please, read on. Yes, it is possible to have the browser back/forward (history) and refresh whilst running a … Read more

Redirect a state to default substate with UI-Router in AngularJS

Update: 1.0 Onwards Supports redirectTo out of the box. https://ui-router.github.io/ng1/docs/latest/interfaces/state.statedeclaration.html#redirectto I created an example here. This solution comes from a nice “Comment” to an an issue with redirection using .when() (https://stackoverflow.com/a/27131114/1679310) and really cool solution for it (by Chris T, but the original post was by yahyaKacem) https://github.com/angular-ui/ui-router/issues/1584#issuecomment-75137373 So firstly let’s extend main with redirection … Read more

Using $window or $location to Redirect in AngularJS

I believe the way to do this is $location.url(‘/RouteTo/Login’); Say my route for my login view was /Login, I would say $location.url(‘/Login’) to navigate to that route. For locations outside of the Angular app (i.e. no route defined), plain old JavaScript will serve: window.location = “http://www.my-domain.example/login”