【问题标题】:How to add route dynamically in angular如何以角度动态添加路线
【发布时间】:2016-01-28 09:43:56
【问题描述】:

我正在写一个 EShop 项目,我在其中展示了一些客户可以购买的产品,当客户点击购买按钮时,我检查客户是否已登录,如果没有,我应该将客户路由到登录过程的特定控制器,如果客户已登录,我将他或她路由到另一个控制器,我该怎么做?

<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>

在我使用window.location 之前,它不是正确的方法,因为我需要角度路由,而且它没有使用这个,任何人都可以告诉我该怎么做?

【问题讨论】:

    标签: angularjs asp.net-mvc routing


    【解决方案1】:

    为此,我可以建议你使用UiRouter:https://github.com/angular-ui/ui-router

    当你实现 uiRouter 之后,你可以使用 service $state

    您的代码可能如下所示:

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

    uiRouter 还有很多其他的优点,你可以在这里看到:https://github.com/angular-ui/ui-router/wiki

    【讨论】:

    • 您基于另一个可能会或可能不会使用的库构建了答案!
    • 感谢您的回答,但我想找到一种在 ngRoute 中使用 $route 服务的方法,而不是关于另一个库
    • 我认为这个答案(注意这是一个建议)是绝对有效的,因为 ui-router 比 ngRoute 有很多优势。
    猜你喜欢
    • 1970-01-01
    • 2021-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-07
    相关资源
    最近更新 更多