如何在UI路由器当前的状态情况下,相关的值绑定? [英] how to bind the relevant value in case of current status in ui-router?

查看:253
本文介绍了如何在UI路由器当前的状态情况下,相关的值绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我重视的数据属性的每个 .STATE 来识别用户(认证或公共)如下(一个状态的例子)

I have attached data attribute in each .state to identify the user (authenticated or public) as following (one state example)

$stateProvider
 .state('admin-panel.public.home', {
    url: '/p',
    templateUrl: 'app/public/home.tmpl.html',
    controller: 'PublicHomeController',
    controllerAs: 'ctrl',
    data: {
        requireLogin: false
    }
});

我需要使用一些州为用户(认证和公共)为例

I need to use the some state for both of user (authenticated and public) as an example

.state('403', {
    url: '/403',
    templateUrl: '403.tmpl.html',
    controller: function($scope, $state, APP, Auth) {
        $scope.app = APP;

        $scope.goHome = function() {
            if(Auth.isAuthenticated()){
                $scope.requireLogin = true;
                $state.go('admin-panel.default.home');
            }
            else{
                $scope.requireLogin = false;
                $state.go('admin-panel.public.home');
            }
        };
    },
    data: {
        requireLogin: $scope.requireLogin
    }                

})

下面,当经过验证的用户访问这种状态我需要通过真正价值 requireLogin:真正的以及当公共用户访问这个状态我需要通过 requireLogin:假。我检查了控制器如上当前用户状态。我怎样才能绑定 $ scope.requireLogin 来的数据属性?

Here when the authenticated user access this state I need to pass the true value to requireLogin: true as well when public user access this state I need to pass the false value as requireLogin: false. I checked the current user status in the controller as above. How can I bind the $scope.requireLogin to data attribute?

在任何 UI路由器的专家请告诉办法解决???

Anyone in expert of ui-router please tell a way to solve???

推荐答案

您可以在一个非常清洁的方式解决问题。让我们先从它被添加到 HTML GlobalCtrl code>标记像 NG-控制器=GlobalCtrl

You can solve your problem in a very cleaner way. Let's start with a global controller example GlobalCtrl which is added to the body or html tag like ng-controller="GlobalCtrl.

这样做将使我们能够保持这个范围 GlobalCtrl 在您的单页角的应用程序(如您使用的是 UI路由器),我们能避免使用 $ rootScope (实际上是模仿的使用 $ rootScope

Doing this will enable us to keep the scope of this GlobalCtrl throughout your single page Angular app (as you are using ui-router) and we can avoid the usage of $rootScope (actually mimicking the usage of $rootScope).

现在,你里面 GlobalCtrl 定义是这样的:

Now, inside your GlobalCtrl define something like this:

// Using an object to avoid the scope inheritance problem of Angular
// https://github.com/angular/angular.js/wiki/Understanding-Scopes
$scope.globalData = {};

// Will be called everytime before you start navigating to any state
$scope.$on('$stateChangeStart', function(event, toState, toParams) {
    $scope.globalData.requireLogin = false;
    var statesToLoginCheck = ['403', 'foo', 'bar']; // and other states on which you want to check if user is logged in or not

    // If the current state on which we are navingating is allowed to check for login
    if (statesToLoginCheck.indexOf(toState.name) > -1) {
        if (Auth.isAuthenticated()) {
            $scope.globalData.requireLogin = true;
            $state.go('admin-panel.default.home');
        } else {
            $scope.globalData.requireLogin = false;
            $state.go('admin-panel.public.home');
        }

        event.preventDefault();
        return;
    }
});

现在,因为 $ GlobalCtrl 范围 HTML 然后每个国家或指令将继承这个 GlobalCtrl 的范围,然后你只需要检查您的变量的任何控制器 $ scope.globalData.requireLogin

Now, since $scope of GlobalCtrl is in body or html then every state or directive will inherit the scope of this GlobalCtrl and then you simply have to check in your any controller of variable $scope.globalData.requireLogin.

这篇关于如何在UI路由器当前的状态情况下,相关的值绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆