Florian
Florian

Reputation: 197

angular ui-router resolve if params set

I would like secure my route access by redirect the user if no params send. to prevent direct url access. I can do that in my controller but i have in my resolve some web service call. the goal is to not call the resolve if the params isn't present. Here my state :

.state('parent.step2', {
    url: '/step2',
    templateUrl: 'app/template/step2.html',
    controller: 'Step2Controller',
    controllerAs: 'step2',
    params: {name: null},
    data : {
        step: 2
    },
    resolve: {
        data1: ['Data1', function(Data1) {
            return Data1.query().$promise;
        }],
        data2: ['Data2', '$stateParams', function(Data2, $stateParams) {
            return Data2.query({name: $stateParams.name}).$promise;
        }],
        data3: ['Data3', function(Data3) {
            return Data3.query().$promise;
        }]
    }
})

Upvotes: 0

Views: 223

Answers (1)

Zakaria
Zakaria

Reputation: 15070

I would have put a listener on the $stateChangeStart event:

$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams){
    if ((!toState.data.step) && ($state.current.name === 'parent.step2')) {
        $state.go('error');
    }
  })

Upvotes: 1

Related Questions