0

I've an SPA with rails api on backend and angular as front-end framework.

I'm using stateProviders, $urlRouterProvider for managing states in my SPA I'm facing a small problem with ui-router. Whenever I refresh by hitting Comand+R from Mac/Linux or F5 from Windows. It goes to the state where it was refreshed and right after it redirects to the \dashboard.

I want to stop this redirect I've read some articles, related question, and some github issues too but nothing made me successful.

Some of references where I did consult.

Angular-ui-router state with $stateParams always redirects to $urlRouteProvider.otherwise

AngularJS : angular-ui-router always redirects to $urlRouterProvider.otherwise location

https://github.com/angular-ui/ui-router/wiki/URL-Routing

After digging into those.

Here is my ui-router code:

app.coffee

'use strict'

  $stateProvider
    .state('app',
      url: '/'
      templateUrl: 'shared/app.html'
      abstract: true
      resolve:
        company: ['CompanyResource', '$rootScope', (CompanyResource, $rootScope) ->
          CompanyResource.current().$promise.then (company)->
            $rootScope.app.company = company
        ]
        user: ['Auth', '$state', '$rootScope', (Auth, $state, $rootScope)->
          if !$rootScope.signedin
            Auth.currentUser()
              .catch ->
                $state.go('login.signin')
        ]
    )
    .state('login',
      template: '<div ui-view></div>'
      abstract: true
      resolve:
        company: ['CompanyResource', '$rootScope', (CompanyResource, $rootScope) ->
          CompanyResource.current().$promise.then (company)->
            $rootScope.app.company = company
        ]
    )
    .state('login.signin',
      url: '/signin'
      templateUrl: 'templates/signin.html'
      controller: 'SigninCtrl'
      controllerAs: 'signin_ctrl'
    )
    .state('app.dashboard',
      url: 'dashboard'
      templateUrl: 'templates/admin/dashboard.html'
      title: 'Dashboard'
      controller: 'DashboardCtrl'
      controllerAs: 'dashboard_ctrl'
      resolve: []
    )
    .state('app.settings',
      url: 'settings'
      templateUrl: 'templates/admin/settings/base.html'
      controller: 'SettingsBaseCtrl'
      controllerAs: 'settings_ctrl'
      resolve:
        settings: ['AdminSettingsResource', (AdminSettingsResource) ->
          AdminSettingsResource.query().$promise
        ]
    )

  $urlRouterProvider.otherwise '/dashboard'

angular
  .module('MYAPP', [
    'templates'
    'ngResource'
    'ngAnimate'
    'ngCookies'
    'ngStorage'
    'ngSanitize'
    'ngTouch'
    'ui.router'
    'ui.bootstrap'
    'duScroll'
    'angularMoment'
    'Devise'
  ])
  .config(['$compileProvider', ($compileProvider)->
    $compileProvider.debugInfoEnabled(true)
  ])
  .run(['$rootScope', '$state', '$stateParams', ($rootScope, $state, $stateParams) ->
    FastClick.attach document.body

    $rootScope.$state = $state
    $rootScope.$stateParams = $stateParams
  ])
  .config([
    '$stateProvider'
    '$urlRouterProvider'
  ])
5
  • Might be related to the inconsistent use of /URLs but I can barely read CoffeeScript. Note that otherwise can be passed a function which might help you track down the issue. Commented Mar 26, 2017 at 10:06
  • Also, your having to write .$promise on all service calls, although an orthogonal issue, indicates incorrect use of $q.derfer() and of Promises in general. This will likely bite you and indicates you are probably writing more code than you have to. Service methods should return a Promise, not a Deferred. Commented Mar 26, 2017 at 10:14
  • 1
    @AluanHaddad Ok for the deffer suggestion But my issue is different thanks for your advice although.. I have converted to javascript and created a gist for you please look into. gist.github.com/mutafaf/3ede1adbcc53a60df93177d3da652d67 Commented Mar 26, 2017 at 10:28
  • Here is a plunkr where I tried to set it up (from your GIST). There is too much crap missing. I cannot stub out a dozen controllers and services. I almost did, here it is (WIP) plnkr.co/edit/8cDj32VDPpZX54dRNZmq?p=info. Sorry, but you cannot expect someone to rewrite all of that logic and invent all of those templates and stub out all of those controllers (I did try but I didn't have time to finish). Good luck to you Commented Mar 26, 2017 at 11:28
  • Also, in addition to being a terrible API, ngResource is awful for questions like this. I had to recreate all of your services stubbing them out with $q dependencies, ripping out your 8+ module('app', [...]) dependencies. That is not even close to a minimal repro. Commented Mar 26, 2017 at 11:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.