从电子邮件链接重定向到AngularJS状态

时间:2018-02-01 13:58:42

标签: javascript angularjs email redirect

我想将用户从电子邮件链接重定向到我的Angular应用中的某个状态。我的应用程序是单页面应用程序,这意味着我不能简单地将用户重定向到状态的URL。

到目前为止,我尝试使用网址参数:

此链接,让我们说去菜单,通过电子邮件发送给用户=&gt; $(document).ready(function(){ $("html, body").animate({ scrollTop: $('#<?php echo $pageSection;?>').offset().top }, 1000); });

在我的应用程序的主控制器中,当用户登陆时,我使用以下JavaScript函数

https://mywebsite.com/index?state=menu

函数 getParam()是:

 function checkRedirectFromEmail(){
    var stateParam = getParam('state');
    $rootScope.gotoState('tabs.'+stateParam);
    // $location.url($location.path()); // I TRIED THIS, NO SUCCESS
    // $location.search('state', null); // I TRIED THIS TOO, NO SUCCESS
}

函数 gotoState()是:

 function getParam(param) {
    var vars = {};
    window.location.href.replace( location.hash, '' ).replace( 
        /[?&]+([^=&]+)=?([^&]*)?/gi, // regexp
        function( m, key, value ) { // callback
            vars[key] = value !== undefined ? value : '';
        }
    );
    if ( param ) {
        return vars[param] ? vars[param] : null;    
    }
    return vars;
}

最后,这里是我的路由逻辑中的重要状态和参数:

 $rootScope.gotoState = function(stateName) {
    $state.go(stateName, {}, { location: false } ); 
};

目前,用户已重定向但

  • 我必须在我的网址中添加“index”才能读取我的参数(通常情况下,它只是 $stateProvider .state('tabs', { url: "/tabs", abstract: true, templateUrl: "templates/tabs.html" }) .state('tabs.index', { url: "/", cache:false, views: { 'index-tab': { templateUrl: "home.html" } } }) }) $urlRouterProvider.otherwise("/"); )。
  • URL对参数保持“脏”,我想清除参数。我尝试了几种方法(参见我的代码),但它无法正常工作。

我的问题:

是否有其他方法可以重定向用户,而不是使用网址参数将用户从电子邮件重定向到我的应用程序的某个状态? 如果不是,有没有办法使它比我现在更清洁(清除网址参数,不必在网址中添加“索引”)?

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我在参与的项目中实现的一个很好的解决方案(电子邮件重定向到特定的应用程序状态)是使用ui-router支持的状态参数来处理它。

$stateProvider
    .state('home', {
        url: "/home/:redirectTo",
        templateUrl: 'home.html',
        controller: function ($stateParams, $state) {

            // based on redirectTo stateparam you can redirect the user to
            // the desired state. eg: /home/results -> results page
            $state.go($stateParams.redirectTo, {} );
        }
    })

因此,当您点击以下链接时:&#34; myIp / home / results&#34;如果状态结果有url&#39;结果&#39;将重定向到myIp / results。

相关问题