pmn
pmn

Reputation: 2255

How to add route dynamically in angular

I'm writing an EShop project, and i show some products in it which customers can buy them, when the customer click on buy button, i checked whether customer was logged in or not, if not i should route the customer to specific controller for log in process, if customer was logged in i route him or her to another controller, how can i do it?

<script>
  app.controller('buyTheItem',function($scope,$http){

    //when customer click on buy button then:
     $scope.buy = function(){
            // here i check whether customer was logged in or not
             if(was logged in){
                // i should route to  buyProductController
             }
             else{
                // i should route to  accountController for log in
              }
         };
   });
</script>

before i use window.location but its not right way because i need angular routing, and it's not use this,, any one can tell how can i do it?

Upvotes: 0

Views: 108

Answers (1)

Konrad Cerny
Konrad Cerny

Reputation: 200

For this, I can suggest to you to use UiRouter: https://github.com/angular-ui/ui-router

When you implement uiRouter you can afterwards use service $state

and your code can look like:

if (was logged in) {
    $state.go('my state for buyProductController');
} else {
    $state.go('my state for accountController');
}

uiRouter has a lot of other advantages, all you can see here: https://github.com/angular-ui/ui-router/wiki

Upvotes: 1

Related Questions