Angular View automatically redirecting to default (otherwise) view

angular-routing, angularjs, javascript, redirect

Solution

I think your response.details has non-url encoded characters making the route provider think that you are navigating to a deeper page i.e. `var response = {details:'bar/foo'};` would in effect be navigating to `/view2/bar/foo` triggering your otherwise.

Problem

My view is automatically redirecting to default (otherwise) view and I have no idea why. When I click, it goes from View1 to View2 and then automatically redirects to default View3. I checked by putting `console.log()` in View2 View1 -> View2 -> View3 ``` myApp.config(function ($stateProvider, $urlRouterProvider) { $stateProvider .state('view1', { url: "/view1", templateUrl: "App/Views/start/view1.html", controller: 'MyController' }) .state('view2', { url: "/view2/:details", templateUrl: "App/Views/start/view2.html", controller: 'MyController' }); $urlRouterProvider.otherwise('/view3'); }); ``` And I am calling `$state.go` like this. ``` $state.go('view2', { details: JSON.stringify(response.details) }); ``` I tried to intercept it by preventing default on `$stateChangeStart` but ran into an error : $digest() iterations reached. aborting which is due to an interaction issue between `$urlRouterProvider` and `$stateProvider` as explained here. But even if I intercept it successfully, I am not sure if that's the correct way. So what is actually causing it? Or How can I find out what's causing the redirect. By the way I have already checked for `$stateChangeError` and there are none. ``` myApp.controller('MyController', ['$scope', '$stateParams', '$state', function ($scope, $stateParams, $state) { $scope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) { if (toState.name == "view3" && fromState.name == "view2") { event.preventDefault(); } }); }); ```

Original source

Related problems