<tfoot id='CK9lS'></tfoot>

  • <legend id='CK9lS'><style id='CK9lS'><dir id='CK9lS'><q id='CK9lS'></q></dir></style></legend>
    <i id='CK9lS'><tr id='CK9lS'><dt id='CK9lS'><q id='CK9lS'><span id='CK9lS'><b id='CK9lS'><form id='CK9lS'><ins id='CK9lS'></ins><ul id='CK9lS'></ul><sub id='CK9lS'></sub></form><legend id='CK9lS'></legend><bdo id='CK9lS'><pre id='CK9lS'><center id='CK9lS'></center></pre></bdo></b><th id='CK9lS'></th></span></q></dt></tr></i><div id='CK9lS'><tfoot id='CK9lS'></tfoot><dl id='CK9lS'><fieldset id='CK9lS'></fieldset></dl></div>

      <small id='CK9lS'></small><noframes id='CK9lS'>

        • <bdo id='CK9lS'></bdo><ul id='CK9lS'></ul>
      1. 在ui-router中的当前状态下如何绑定相关值?

        时间:2023-10-21
          <bdo id='u9EDx'></bdo><ul id='u9EDx'></ul>
            <tbody id='u9EDx'></tbody>

            <small id='u9EDx'></small><noframes id='u9EDx'>

              <i id='u9EDx'><tr id='u9EDx'><dt id='u9EDx'><q id='u9EDx'><span id='u9EDx'><b id='u9EDx'><form id='u9EDx'><ins id='u9EDx'></ins><ul id='u9EDx'></ul><sub id='u9EDx'></sub></form><legend id='u9EDx'></legend><bdo id='u9EDx'><pre id='u9EDx'><center id='u9EDx'></center></pre></bdo></b><th id='u9EDx'></th></span></q></dt></tr></i><div id='u9EDx'><tfoot id='u9EDx'></tfoot><dl id='u9EDx'><fieldset id='u9EDx'></fieldset></dl></div>
                <tfoot id='u9EDx'></tfoot>

                • <legend id='u9EDx'><style id='u9EDx'><dir id='u9EDx'><q id='u9EDx'></q></dir></style></legend>
                  本文介绍了在ui-router中的当前状态下如何绑定相关值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在每个 .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
                      }                
                  
                  })
                  

                  在这里,当经过身份验证的用户访问此状态时,我需要将 true 值传递给 requireLogin: true 以及当公共用户访问此状态时,我需要传递 false值为 requireLogin: false.如上所述,我在 controller 中检查了当前用户状态.如何将 $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-router高手请指教解决办法???

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

                  推荐答案

                  你可以用一种非常干净的方式解决你的问题.让我们从添加到 bodyhtml 标签的全局控制器示例 GlobalCtrl 开始,例如 ng-controller="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.

                  这样做将使我们能够在整个单页 Angular 应用程序中保持此 GlobalCtrl 的范围(当您使用 ui-router 时),我们可以避免使用$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$scopebodyhtml 中,那么每个状态或指令都会继承此 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-router中的当前状态下如何绑定相关值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                      1. <i id='vbOsK'><tr id='vbOsK'><dt id='vbOsK'><q id='vbOsK'><span id='vbOsK'><b id='vbOsK'><form id='vbOsK'><ins id='vbOsK'></ins><ul id='vbOsK'></ul><sub id='vbOsK'></sub></form><legend id='vbOsK'></legend><bdo id='vbOsK'><pre id='vbOsK'><center id='vbOsK'></center></pre></bdo></b><th id='vbOsK'></th></span></q></dt></tr></i><div id='vbOsK'><tfoot id='vbOsK'></tfoot><dl id='vbOsK'><fieldset id='vbOsK'></fieldset></dl></div>
                        <tfoot id='vbOsK'></tfoot>
                          <bdo id='vbOsK'></bdo><ul id='vbOsK'></ul>

                          <small id='vbOsK'></small><noframes id='vbOsK'>

                            <tbody id='vbOsK'></tbody>
                          <legend id='vbOsK'><style id='vbOsK'><dir id='vbOsK'><q id='vbOsK'></q></dir></style></legend>