Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dont instantiate the controller before validate user #200

Closed
NetanelBasal opened this issue Aug 26, 2015 · 2 comments
Closed

Dont instantiate the controller before validate user #200

NetanelBasal opened this issue Aug 26, 2015 · 2 comments

Comments

@NetanelBasal
Copy link

My situation is like this:

  1. I have two api's, one for auth users and one for non auth users.
  2. In the controller i have function that send http request to the right api based if the user is authenticated or not.

The problem is the controller instantiate and send the request before the verify auth is finish the check and always send the http call to non authenticated api.

How i solve this problem?

@markhaasjes
Copy link

In the case you're using ui-router, you can use resolves to provide the controller with data before it's instantiated.

If any of these dependencies are promises, they will be resolved and converted to a value before the controller is instantiated and the $stateChangeSuccess event is fired.

$stateProvider.state('myState', {
      resolve:{
         // Example using function with returned promise.
         // This is the typical use case of resolve.
         // You need to inject any services that you are
         // using, e.g. $http in this example
         promiseObj:  function($http){
            // $http returns a promise for the url data
            return $http({method: 'GET', url: '/someUrl'});
         }
      }
});

Good luck!

@booleanbetrayal
Copy link
Collaborator

The standard angular router also supports resolver promises. This pattern is the best way to go about this, IMO.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants