我有一个有2个ui视图的SPA:头部和身体。头部由一个控制器运行,处理主体的控制器(取决于状态)调用我创建的服务函数,如下所示:
/**
* Sets the header bar's attributes
*
* @param inverse bool Whether the color scheme should be inverted
* @param button bool Whether the back button should be presented
* @param link string The route the back button will take the user to when pressed
* @param title string The translation key to use for the title to be shown in the headerbar
* @param data string Characters to be inserted in the span next to the title
*
**/
return {
set: function(inverse, button, link, title, data) {
// set defaults
inverse = typeof inverse !== 'undefined' ? inverse : false;
button = typeof button !== 'undefined' ? button : false;
link = typeof link !== 'undefined' ? link : $state.current.name;
title = typeof title !== 'undefined' ? title : undefined;
data = typeof data !== 'undefined' ? data : false;
// store for HeaderController to retrieve
$localStorage.headerInverse = inverse;
$localStorage.headerBackButton = button;
$localStorage.headerBackLink = link;
$localStorage.headerTitle = title;
$localStorage.headerData = data;
broadcast.announce('setHeader');
}
};
一样:
header.set(true, true, 'app.search1', 'MY_PROFILE');
报头的控制器用:
监听广播 broadcast.listenFor('setHeader', function() {
vm.headerTitle = $localStorage.headerTitle; // expects the translation key to look up
vm.headerData = $localStorage.headerData;
vm.headerInverse = $localStorage.headerInverse;
vm.headerBackLink = $localStorage.headerBackLink;
vm.headerBackButton = $localStorage.headerBackButton;
vm.headerMoreIcon = $localStorage.headerMoreIcon;
});
标题的html有:
<a ui-sref="{{ vm.headerBackLink }}"></a>
所以我期望的是:
<a href="#/search1" ui-sref="app.search1"></a>
然而我得到的渲染是:
<a href="#/profile" ui-sref="app.search1"></a>
任何帮助都会很感激。谢谢!
与其动态传递状态名,不如试试这样做:
$scope.getLinkUrl = function(){
return $state.href('state-name', {someParam: $scope.someValue});
};
<a ng-href="{{getLinkUrl()}}">Dynamic Link</a>
Ref:动态设置ui-sref Angularjs的值