muffel
muffel

Reputation: 7380

Unwanted print error of rejected promise for state transition of ui-router to console

I am using the resolve property of ui-routers state definition for angular 1 to provide a function which returns a promise. The provided function should check if the user is allowed to visit the state or not.

If the returned promise gets rejected, the state transition does not happen - just what I want to achieve. Additionally I can listen for the $stateChangeError event to get details about the rejection. All works fine.

My problem is that the error is printed to the console what I want to avoid.

Consider this state definiton:

$stateProvider.state({
    name: 'about',
    url: '/about',
    template: '<h3>Its the UI-Router hello world app!</h3>',
    resolve: {
      isAllowed:function(){
        return new Promise(function(resolve, reject){
          reject(new Error("not allowed"));
        });
      }
    }
  });

which results in the following error printed to the console, whenever this state is visited:

console error

Please see this Plunker for a minimal working example.

Is there any way to get rid of the console error while maintaining the rest of the behavior?

Upvotes: 1

Views: 441

Answers (3)

Saurabh Agrawal
Saurabh Agrawal

Reputation: 7739

It could be because of version of angular-ui-router you are using. Try This version (0.4.2). You wont get error in this version.

Upvotes: 2

Shabbir Hussain
Shabbir Hussain

Reputation: 56

You can use alternate way like this:

$rootScope.$on('$stateChangeStart', function (evt, toState,
toParams, fromState, fromParams, options) {
    if(not_allowed) {
       evt.preventDefault();
    }
}

Upvotes: 1

Sravan
Sravan

Reputation: 18647

Since the version of ui-router you used [email protected], is in the BETA version, there are few console.log lines are left in their scripts.

So, you can use the more stable version to overcome this issue, for example you can use [email protected]

This gets rid of the console error while maintaining the rest of the behavior

Here is a new plunker

Upvotes: 1

Related Questions