Javascript 从电子邮件链接重定向到AngularJS状态
我想将我的用户从电子邮件链接重定向到我的Angular应用程序中的某个状态。我的应用程序是一个单页应用程序,这意味着我不能简单地将用户重定向到该州的URL 到目前为止,我尝试使用URL参数:Javascript 从电子邮件链接重定向到AngularJS状态,javascript,angularjs,email,redirect,Javascript,Angularjs,Email,Redirect,我想将我的用户从电子邮件链接重定向到我的Angular应用程序中的某个状态。我的应用程序是一个单页应用程序,这意味着我不能简单地将用户重定向到该州的URL 到目前为止,我尝试使用URL参数: $stateProvider .state('tabs', { url: "/tabs", abstract: true, templateUrl: "templates/tabs.html" }) .state('tabs.index', { url: "/"
$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("/");
这个链接,比方说转到菜单,通过电子邮件发送给用户=>https://mywebsite.com/index?state=menu
在我的应用程序的主控制器中,当用户登陆时,我使用以下JavaScript函数:
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
}
其中函数getParam()是:
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 } );
};
函数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 } );
};
最后,这里是我的路由逻辑中的重要状态和参数:
$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中添加“索引”,以便读取我的参数(通常是
)https://mywebsite.com/#/
- URL仍然带有参数“脏”,我想清除这些参数。我尝试了几种方法(请参见我的代码),但都没有正常工作
$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参数将用户从电子邮件重定向到我的应用程序的特定状态之外,还有其他重定向用户的方法吗如果没有,有没有办法让它比我现在拥有的更干净(清除url参数,不必在url中添加“索引”)
感谢您的帮助。我在参与的项目中实现了一个很好的解决方案(电子邮件重定向到特定的应用程序状态),就是使用ui路由器支持的状态参数来处理这个问题
$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, {} );
}
})
因此,当您单击类似“myIp/home/results”的链接时,如果状态结果具有url“results”,则会将您重定向到myIp/results。以更好的方式使用角度组件通过链接传递值使用HTML5模式(true)要删除散列并添加到html文件中,您使用的是angularjs默认路由器还是ui路由器?@praveenkumars我尝试过,但现在无法识别我的URL参数(我的getParam函数返回null)。@VarvarigoSemmanuil我使用的是带ui路由器的Ionic framework。