2016-05-03 10 views
1

私はhtmlコード(コードスニペット1)&以下のようにJSコード(コードスニペット2)を持っています。 htmlで部分的に を追加し、状態が変わったときにhtmlを動的に変更するui-viewを含むページを構築しています。 これは正常に動作しています。このページには、ヘッダー、フッター、左側のメニュー、および状態に基づいて動的に変化するコンテンツが含まれています( )。 このアプリケーションのログインページを含める必要があります。しかし、我々は ヘッダー、フッター、ログイン画面のメニューを必要としません。 現在、ログイン画面は、header、footer、menuと共に定義された単一のui-viewに表示されます。AngularJS UI-Router +状態ヘルプが必要

私はログイン用に2つのdivsを定義でき、状態がログインの場合はそのdivを表示できると思っていました。状態がログインでない場合、下のhtmlコードに表示されているdivを表示します(スニペット1)。これが実現可能かどうかをアドバイスしてください。もしそうなら、htmlで州にアクセスする方法を教えてください。下記のコード(スニペット3)を使用して状態にアクセスしようとしましたが、アドバイスできませんでした。

HTML(コードスニペット1):

<code> 
    <div class="grandContainer">  
    <div ng-include="'${header}'"></div> 
    <div class="abc"> 
      <div ng-include="'${status}'"></div> 
      <div ng-include="'${menu}'"></div> 
    </div>  
    <div class="mainContainer"> 
    <div ui-view></div> 
    </div> 

    <p ng-include="'${footer}'"></p> 
</div> 
</code> 

JS(コードスニペット2):

app.config(['$stateProvider','$urlRouterProvider',function($stateProvider, $urlRouterProvider) { 
    $urlRouterProvider.otherwise('/login'); 
    $stateProvider.state('grid', { 
    url: '/grid', 
    templateUrl: 'resources/html/grid.html', 
    controller: 'gridCtrl' 
    }).state('chDetail', { 
    url: "/chDetail/:chId", 
    templateUrl: "resources/html/chDetail.html", 
    controller: 'chDetailCtrl' 
    }).state('login', { 
    url: "/login", 
    templateUrl: "resources/html/login.html", 
    controller: 'loginCtrl' 
    }); 
    }]).controller('gridCtrl', ['$scope', function($scope){ 
    $scope.grid =[]; 
    }]); 

</code> 

code snippet 3:-

<code> 

<div ng-show="{$state.is('login')}">Item</div> 
</code> 

答えて

1

If I've understood it correctly you want to display the login view with-out the other views like header, footer & side menu.

Then you could create an abstract state for the layout with your views headerfooterside-menuなど、ログインビューのための別の状態を追加します。

デモのトリッキーな部分は抽象状態でのビューの定義である:

abstract: true, 
views: { 
    '': { 
     templateUrl: 'layout.html', 
    }, 
    '[email protected]': { 
     template: 'content for header' 
    }, 
    '[email protected]': {...} 
} 

抽象状態は何をしているのですか?抽象状態では、メインテンプレートの名前のないビューにレイアウトテンプレートが読み込まれ、子の状態と共有したい他のビューが読み込まれます。

各子状態は、必要に応じてコンテンツビューを定義し、他のビューを再利用することができます。

以下のデモまたはfiddleをご覧ください。

注: デモのログインは安全ではなく、ビューのリダイレクト処理を表示するだけです。あなたのアプリでは$ httpとsessionStorageとトークンで作業する必要があります。

angular.module('demoApp', ['ui.router']) 
 
\t .run(function($rootScope, $state) { 
 
      $rootScope.$on('$stateChangeError', function(evt, to, toParams, from, fromParams, error) { 
 
      if (error.redirectTo) { 
 
      $state.go(error.redirectTo); 
 
      } else { 
 
      $state.go('error', {status: error.status}) 
 
      } 
 
     }) 
 
    }) 
 
\t .factory('userService', userService) 
 
\t .config(routes); 
 

 
function userService() { 
 
\t var usersMock = { 
 
    \t 'testUser': { 
 
     \t username: 'testUser', 
 
      password: '1234' 
 
     }, 
 
     'testUser2': { 
 
     \t username: 'testUser2', 
 
      password: '1234' 
 
     } 
 
    }; 
 
\t var userService = { 
 
    \t user: undefined, 
 
    \t login: function(userCredentials) { 
 
     \t // later --> $http.post('auth', userCredentials).then(...) 
 
      // for demo use local data 
 
      var user = usersMock[userCredentials.username] 
 
      userService.user = (user && (user.password == userCredentials.password)) ? 
 
      \t user : undefined; 
 
      return user; 
 
     }, 
 
     logout: function() { 
 
     \t userService.user = undefined; 
 
     } 
 
    } 
 
    
 
    return userService; 
 
} 
 
function routes($urlRouterProvider, $stateProvider) { 
 

 
\t $urlRouterProvider.otherwise('/'); 
 
    
 
    $stateProvider 
 
    \t .state('root', { 
 
     \t url: '', 
 
     \t abstract:true, 
 
      resolve: { 
 
      \t 'user': function(userService) { 
 
       \t return userService.user; // would be async in a real app 
 
       } 
 
      }, 
 
      views: { 
 
      \t '': { 
 
       \t templateUrl: 'layout.html', 
 
       }, 
 
      \t '[email protected]': { 
 
       \t template: '<h1>header View<span ng-if="user"><button ng-click="logout()">logout</button></span><span ng-if="!user"><button ng-click="login()">login</button></span></h1>', 
 
        controller: function($scope, $state, user, userService) { 
 
        \t $scope.user = user; 
 
         $scope.login = function() { 
 
         \t $state.go('login'); 
 
         }; 
 
         $scope.logout = function() { 
 
         \t userService.logout(); 
 
          $state.go('root.home', {}, {reload: true}); 
 
         }; 
 
        } 
 
      \t }, 
 
       '[email protected]': { 
 
        template: '<p>footer view</p>' 
 
       } 
 
      } 
 
     }) 
 
    \t .state('root.home', { 
 
      url: '/', 
 
      views: { 
 
       'content': { 
 
        template: 'Hello at home' 
 
       } 
 
      } 
 
     }) 
 
     .state('root.about', { 
 
      url: '/about', 
 
      views: { 
 
       'content': { 
 
        template: 'about view' 
 
       } 
 
      } 
 
    \t }) 
 
     .state('root.restricted', { 
 
      url: '/restricted', 
 
      resolve: { 
 
      \t auth: function(userService, $q, $timeout) { 
 
       
 
       \t var deferred = $q.defer(); 
 
       \t /* //with an async 
 
        return UserService.load().then(function(user){ 
 
         if (permissionService.can(user, {goTo: state})) { 
 
         return deferred.resolve({}); 
 
         } else { 
 
         return deferred.reject({redirectTo: 'some_other_state'}); 
 
         } 
 
        }*/ 
 
        
 
        $timeout(function() { 
 
         if (angular.isUndefined(userService.user)) { 
 
          return deferred.reject({redirectTo: 'login'}); 
 
         } 
 
         else { 
 
          return deferred.resolve(userService.user); 
 
         } 
 
        }); 
 
        
 
        return deferred.promise; 
 
       } 
 
      }, 
 
      views: { 
 
       'content': { 
 
        template: 'this is only visible after login. Hello {{user}}!', 
 
        controller: function($scope, auth) { 
 
        \t $scope.user = auth.username; 
 
        } 
 
       } 
 
      } 
 
    \t }) 
 
     .state('login', { 
 
      url: '/login', 
 
      templateUrl: 'views/login.html', 
 
      controller: function($scope, $state, userService) { 
 
      \t $scope.login = function(cred) { 
 
       \t var user = userService.login(cred); 
 
        
 
        if (angular.isUndefined(user)) { 
 
        \t alert('username or password incorrect.') 
 
        } 
 
        else { 
 
        \t $state.go('root.restricted'); 
 
        } 
 
       }; 
 
      } 
 
     }); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.18/angular-ui-router.js"></script> 
 

 
<div ng-app="demoApp"> 
 
    <a ui-sref="root.home" href="#">home</a> 
 
    <a ui-sref="root.about" href="#">about</a> 
 
    <a ui-sref="root.restricted" href="#">restricted page</a> 
 
    
 
    <div ui-view> 
 
    </div> 
 
    
 
    <script type="text/ng-template" id="views/login.html"> 
 
    \t <h1>Login</h1> 
 
     <p>Try testUser and 1234 as credentials</p> 
 
     <label>Username</label><input ng-model="userCred.username"/> 
 
     <label>Password</label><input type="password" ng-model="userCred.password"/> 
 
     <button ng-click="login(userCred)">login</button> 
 
    </script> 
 
    
 
    <script type="text/ng-template" id="layout.html"> 
 
    \t <div> 
 
      <div ui-view="header"></div> 
 
      <div ui-view="content"></div> 
 
      <div ui-view="footer"></div> 
 
    \t </div> 
 
    </script> 
 
</div>

+0

迅速な対応をありがとうございます。これを試してみましょう。 – user3504187

+0

ありがとう、私はUIを修正することができました。 – user3504187

+0

このコードは何をしていますか? .run(function($rootScope, $state) { $rootScope.$on('$stateChangeError', function(evt, to,toParams, from, fromParams, error) { if (error.redirectTo) { $state.go(error.redirectTo); } else { $state.go('error', {status: error.status}) } }) user3504187

0

very basically you can use $state for get current state like:

module.exports = function Controller($state, $scope) 

$scope.currentState = $state.current; 

then in your html

<p ng-if="currentState !='login'" ng-include="'${footer}'"></p> 

but nested ui-router logic could be more useful.

0
app.config(['$stateProvider','$urlRouterProvider', 
    function($stateProvider, $urlRouterProvider) { 
    $urlRouterProvider.otherwise('/login'); 
    $stateProvider.state('grid', { 
     url: '/grid', 
     templateUrl: 'resources/html/grid.html', 
     controller: 'gridCtrl' 
    }) 
    .state('chDetail', { 
     url: "/chDetail/:chId", 
     templateUrl: "resources/html/chDetail.html", 
     controller: 'chDetailCtrl' 
    }) 
    .state('login', { 
     url: "/login", 
     controller: 'loginCtrl', 
     views: { 
     '': { 
      templateUrl: 'resources/html/login.html' 
     }, 
     '[email protected]': { 
      templateUrl: 'resources/html/other-html.html' 
     } 
     } 
    }); 
    } 
]) 
.controller('gridCtrl', ['$scope', function($scope){ 
    $scope.grid =[]; 
}]); 

I've altered it slightly. To get what you want, use views.

Then your extra bit of code would just be another ui-view, just a named one:

<div ui-view="login-only"></div> 

It would only appear on the 'login' state, since you specifically target it via it's name ([email protected]).

You can read more about it at the Multiple Named Views on the ui-router wiki help pages at github.

+0

私は私の要件を修正再表示します。 – user3504187

+0

この問題を解決するのを手伝ってくれてありがとう。私の要求を再確認させてください。 (1)login urlはヘッダー、フッターまたはメニューのないログインページのみを表示すべきです。 (2)他のURLはアプリケーション関連のURLで、HTMLコードスニペット1に表示されているdiv内のヘッダー、フッター、メニューと共に表示されます。したがって、基本的に2ページのレイアウトが異なります。上記のコードスニペットを提供しましたが、私の要件を満たしていません。 htmlのレンダリングを制御する方法をアドバイスしてください。ログイン状態といつ他の状態になるか。 – user3504187