LoginSignup
3
3

More than 5 years have passed since last update.

UI-Routerを使ってみる

Last updated at Posted at 2015-04-08

概要

AngularJSのngRouterの代替用のモジュール。
複数のviewを持てるようになっている。

準備

AngularJSの開発環境を整える

こちらを参考にセットアップする。

UI-Routerを組み込む

$ bower install angular-ui-router --save

使ってみる

テンプレート

<div ui-view="view1"></div>
<div ui-view="view2"></div>

モジュールの登録

angular.module('myApp', ['ui.router']);

ルーティング設定

angular.module('myApp')
    .config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) {
        $stateProvider
            .state('state1', {
                url: '/state1',
                views: {
                    'view1': {
                        templateUrl: 'views/test1.html'
                    },
                    'view2': {
                        templateUrl: 'views/test2.html'
                    }
                }
            })
            .state('state2', {
                url: '/state2',
                views: {
                    'view1': {
                        templateUrl: 'views/test2.html',
                        controller: 'Test2Ctrl'
                    },
                    'view2': {
                        templateUrl: 'views/test1.html',
                        controller: 'Test1Ctrl'
                    }
                }
            });

        $urlRouterProvider.otherwise('/state1');
    }]);

遷移

angular.module('myApp')
    .controller('myCtrl', function($scope, $state) {
        $state.go('state1');
    });

現在の情報を取得

イベント

参考

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3