Injecting $state (ui-router) into $http interceptor causes circular dependency

The Fix Use the $injector service to get a reference to the $state service. var interceptor = [‘$location’, ‘$q’, ‘$injector’, function($location, $q, $injector) { function success(response) { return response; } function error(response) { if(response.status === 401) { $injector.get(‘$state’).transitionTo(‘public.login’); return $q.reject(response); } else { return $q.reject(response); } } return function(promise) { return promise.then(success, error); } }]; … Read more

How can I fix ‘Maximum call stack size exceeded’ AngularJS

I’ve created an example, playing with default pages and auth/unauth user. Similar issue could be seen here Firstly this would be the listener: app.run(function ($rootScope, $state, $location, Auth) { $rootScope.$on(‘$stateChangeStart’, function (event, toState, toParams, fromState) { var shouldLogin = toState.data !== undefined && toState.data.requireLogin && !Auth.isLoggedIn ; // NOT authenticated – wants any private stuff … Read more

Angularjs ui-router abstract state with resolve

Solution here is to distinguish the names of the resolved data. Check this answer: angular ui-route state parent and resolve (nested resolves) And its plunker So in our case we would need this change in parent resolve: { dataParent: [‘$stateParams’, ‘ProfileService’, function ($stateParams, ProfileService) { var username = $stateParams.username; return ProfileService.getProfile(username); }] } and this … Read more

How would I have ui-router go to an external link, such as google.com?

Angular-ui-router doesn’t support external URL, you need redirect the user using either $location.url() or $window.open() I would suggest you to use $window.open(‘http://www.google.com’, ‘_self’) which will open URL on the same page. Update You can also customize ui-router by adding parameter external, it can be true/false. $stateProvider .state(‘external’, { url: ‘http://www.google.com’, external: true }) Then configure … Read more

Angularjs ui-router. How to redirect to login page

The point is, do not redirect if not needed === if already redirected to intended state. There is a working plunker with similar solution .run(function($rootScope, $location, $state, authenticationSvc) { $rootScope.$on( ‘$stateChangeStart’, function(e, toState , toParams , fromState, fromParams) { var isLogin = toState.name === “login”; if(isLogin){ return; // no need to redirect } // now, … Read more

Angular – ui-router get previous state

I use resolve to save the current state data before moving to the new state: angular.module(‘MyModule’) .config([‘$stateProvider’, function ($stateProvider) { $stateProvider .state(‘mystate’, { templateUrl: ‘mytemplate.html’, controller: [“PreviousState”, function (PreviousState) { if (PreviousState.Name == “mystate”) { // … } }], resolve: { PreviousState: [“$state”, function ($state) { var currentStateData = { Name: $state.current.name, Params: angular.copy($state.params), URL: … Read more