[英]how do return the $state.current.name from ui-router statechange
I would like to return the .state('name')
when I change location in angular. 当我以角度改变位置时,我想返回
.state('name')
。
From my run()
it can return the $state
object: 从我的
run()
它可以返回$state
对象:
.run(function($rootScope, Analytics, $location, $stateParams, $state) {
console.log($state);
but when I try to get $state.current
it is empty object 但是当我尝试获取
$state.current
它是空对象
.run(function($rootScope, $location, $stateParams, $state) {
console.log($state.current);
config example: 配置示例:
.config(function($stateProvider, $urlRouterProvider, AnalyticsProvider) {
$urlRouterProvider.otherwise('/');
$stateProvider
.state('home', {
url: '/',
views: {
'': {
templateUrl: 'views/main.html',
controller: 'MainCtrl'
},
'navigation@home': {
templateUrl: 'views/partials/navigation.html',
controller: 'NavigationCtrl'
},
'weekly@home': {
templateUrl: 'views/partials/weekly.html',
controller: 'WeeklyCtrl'
},
'sidepanel@home': {
templateUrl: 'views/partials/side-panel.html',
controller: 'SidePanelCtrl'
},
'shoppanel@home': {
templateUrl: 'views/partials/shop-panel.html',
controller: 'ShopPanelCtrl'
},
'footer@home': {
templateUrl: 'views/partials/footer.html',
controller: 'FooterCtrl'
}
}
})
You can listen for '$stateChangeSuccess' and set state accrodingly. 您可以监听'$ stateChangeSuccess'并按位置设置状态。 For example -
例如 -
$rootScope.$on('$stateChangeSuccess',
function(event, toState, toParams, fromState, fromParams) {
$state.current = toState;
}
)
As an alternative to Sandeep's solution, you can also do it this way: 作为Sandeep解决方案的替代方案,您也可以这样做:
angular.module('yourApp').run(['$rootScope', '$state',
function ($rootScope, $state) {
$rootScope.$state = $state;
}
]);
Doing it this way, it only gets set once instead of on every stateChange. 这样做,它只被设置一次而不是每个 stateChange。 Basically you just store a reference to $state somewhere - I chose $rootScope just to keep this example simple.
基本上你只是存储一个$ state的引用 - 我选择$ rootScope只是为了让这个例子保持简单。
Now in my code I can easily reference it like: 现在在我的代码中,我可以很容易地引用它:
<div ng-show="$state.includes('accounting.dashboard')"></div>
and 和
<div>Current State: {{$state.current.name}}</div>
I also generally store $stateParams as well so I have even more information about the state (but I left it out of this example). 我也通常存储$ stateParams,所以我有更多关于状态的信息(但我把它留在了这个例子中)。
You can also try. 你也可以试试。
.controller('yourApp', function($scope,$state) {
$scope.state = $state;
Now you can log it in your console. 现在您可以在控制台中记录它。
console.log($state);
It should return the current state you are in. 它应该返回您当前的状态。
Or you can call the scope in your html view like this. 或者您可以像这样在html视图中调用范围。
{{state.current.name}}
It will also return de state you are in. 它也将返回你所处的州。
If you want to console your state in the controller then you should console like that: 如果你想在控制器中控制你的状态,你应该这样控制:
console.log($state.current.name);
And if you want to console it in views then: 如果你想在视图中控制它,那么:
In controller : 在控制器中:
$scope.state = $state.current.name;
In Template: print like that 在模板:打印像那样
{{state}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.