2017-03-01 75 views
1

入口点到我的应用程序是一个未知参数值的网址,例如:访问初始参数值

http://www.host.com/key/25 

和我的简化的配置是这样的:

$stateProvider.state('home', { 
     url: "/key/:key", 
     component: 'home' 
}).state('sitenotice', { 
     url: "/sitenotice/", 
     component: 'sitenotice' 
}); 

我尝试在用的index.html导航到“网站通知”,回到“主”:

<html ng-app="myApp"> 
    ... 
    <div ui-view></div> 

    <a ui-sref="sitenotice">site notice</a> 
    <a ui-sref="home">home</a> 
</html> 

它不起作用,因为我没有将键参数传递给“home”状态。我不知道如何访问初始值。或者它可以以某种方式被缓存?是否有可能只用ui-router做到这一点,我还没有使用根控制器。

我使用角度1.5.8和角度路由器1.0.0-beta.3。

+1

后,您必须获取参数(:钥匙)从stateParams,你可以把它在根视镜或通过它到下一个状态。 – Pengyy

回答

0

我的解决方案是使用rootScope并在那里存储参数值。我还更新了angular-ui-router 1.0.0-rc.1。当离开“home”状态时,$ rootScope.navKey获取当前状态参数值。

angular 
.module('myApp') 
.run(function ($rootScope, $stateParams, $transitions) { 
    $rootScope.navKey = ""; 

    $transitions.onExit({ exiting: 'home' }, function() { 
    if (angular.isDefined($stateParams.key)) { 
     $rootScope.navKey = $stateParams.key; 
    } 
    }); 
}); 

在我的index.html我用navKey回到“主”状态:

<a ui-sref="home({ key: navKey })"> 
    <md-button md-no-ink="" class="md-primary">Home</md-button> 
</a>