AngularJS
ngRouteを使用したルーティング
サーチ…
備考
ngRouteはngRouteモジュールで、角度アプリのルーティングとディープリンクサービスとディレクティブを提供します。
ngRouteに関する完全なドキュメントはhttps://docs.angularjs.org/api/ngRouteにあります。
基本的な例
この例では、3つのルートを持つ小さなアプリケーションを設定し、それぞれに独自のビューとコントローラがあり、 controllerAs構文を使用してcontrollerAsます。
ルータはangular .config関数で設定します
-
$routeProviderを.config$routeProviderし.config -
.whenメソッドでルート名をルート定義オブジェクトで定義します。 -
.whenメソッドに、templateまたはtemplateUrl、controllerおよびcontrollerAs指定するオブジェクトを指定します
app.js
angular.module('myApp', ['ngRoute'])
.controller('controllerOne', function() {
this.message = 'Hello world from Controller One!';
})
.controller('controllerTwo', function() {
this.message = 'Hello world from Controller Two!';
})
.controller('controllerThree', function() {
this.message = 'Hello world from Controller Three!';
})
.config(function($routeProvider) {
$routeProvider
.when('/one', {
templateUrl: 'view-one.html',
controller: 'controllerOne',
controllerAs: 'ctrlOne'
})
.when('/two', {
templateUrl: 'view-two.html',
controller: 'controllerTwo',
controllerAs: 'ctrlTwo'
})
.when('/three', {
templateUrl: 'view-three.html',
controller: 'controllerThree',
controllerAs: 'ctrlThree'
})
// redirect to here if no other routes match
.otherwise({
redirectTo: '/one'
});
});
次に、私たちのHTMLでは、 <a>要素をhrefで使用してナビゲーションを定義し、 helloRouteというルート名に対しては、 <a href="#/helloRoute">My route</a>として<a href="#/helloRoute">My route</a>
我々はまた、コンテナやディレクティブと私たちのビューを提供ng-view私達のルートを注入するが。
index.html
<div ng-app="myApp">
<nav>
<!-- links to switch routes -->
<a href="#/one">View One</a>
<a href="#/two">View Two</a>
<a href="#/three">View Three</a>
</nav>
<!-- views will be injected here -->
<div ng-view></div>
<!-- templates can live in normal html files -->
<script type="text/ng-template" id="view-one.html">
<h1>{{ctrlOne.message}}</h1>
</script>
<script type="text/ng-template" id="view-two.html">
<h1>{{ctrlTwo.message}}</h1>
</script>
<script type="text/ng-template" id="view-three.html">
<h1>{{ctrlThree.message}}</h1>
</script>
</div>
経路パラメータの例
この例では、ルートでパラメータを渡す基本的な例をコントローラで使用するために拡張しています
これを行うには、次のことが必要です。
- ルート名にパラメータの位置と名前を設定する
- コントローラ内に
$routeParamsサービスを$routeParams
app.js
angular.module('myApp', ['ngRoute'])
.controller('controllerOne', function() {
this.message = 'Hello world from Controller One!';
})
.controller('controllerTwo', function() {
this.message = 'Hello world from Controller Two!';
})
.controller('controllerThree', ['$routeParams', function($routeParams) {
var routeParam = $routeParams.paramName
if ($routeParams.message) {
// If a param called 'message' exists, we show it's value as the message
this.message = $routeParams.message;
} else {
// If it doesn't exist, we show a default message
this.message = 'Hello world from Controller Three!';
}
}])
.config(function($routeProvider) {
$routeProvider
.when('/one', {
templateUrl: 'view-one.html',
controller: 'controllerOne',
controllerAs: 'ctrlOne'
})
.when('/two', {
templateUrl: 'view-two.html',
controller: 'controllerTwo',
controllerAs: 'ctrlTwo'
})
.when('/three', {
templateUrl: 'view-three.html',
controller: 'controllerThree',
controllerAs: 'ctrlThree'
})
.when('/three/:message', { // We will pass a param called 'message' with this route
templateUrl: 'view-three.html',
controller: 'controllerThree',
controllerAs: 'ctrlThree'
})
// redirect to here if no other routes match
.otherwise({
redirectTo: '/one'
});
});
次に、カスタムメッセージで新しいリンクを追加するだけで、テンプレートの変更を行いながら、新しいカスタムメッセージが表示されます。
index.html
<div ng-app="myApp">
<nav>
<!-- links to switch routes -->
<a href="#/one">View One</a>
<a href="#/two">View Two</a>
<a href="#/three">View Three</a>
<!-- New link with custom message -->
<a href="#/three/This-is-a-message">View Three with "This-is-a-message" custom message</a>
</nav>
<!-- views will be injected here -->
<div ng-view></div>
<!-- templates can live in normal html files -->
<script type="text/ng-template" id="view-one.html">
<h1>{{ctrlOne.message}}</h1>
</script>
<script type="text/ng-template" id="view-two.html">
<h1>{{ctrlTwo.message}}</h1>
</script>
<script type="text/ng-template" id="view-three.html">
<h1>{{ctrlThree.message}}</h1>
</script>
</div>
個々のルートのカスタム動作の定義
個々のルートのカスタム動作を定義する最も簡単な方法はかなり簡単です。
この例では、ユーザーを認証するためにこの例を使用します。
1) routes.js :任意の目的のルートに対して新しいプロパティ( requireAuth )を作成する
angular.module('yourApp').config(['$routeProvider', function($routeProvider) {
$routeProvider
.when('/home', {
templateUrl: 'templates/home.html',
requireAuth: true
})
.when('/login', {
templateUrl: 'templates/login.html',
})
.otherwise({
redirectTo: '/home'
});
}])
2)内部要素に結合していないトップ層のコントローラでng-view (角との競合を回避するために$routeProvider )かどうかを確認newUrl有するrequireAuthプロパティをし、それに応じて行動します
angular.module('YourApp').controller('YourController', ['$scope', 'session', '$location',
function($scope, session, $location) {
$scope.$on('$routeChangeStart', function(angularEvent, newUrl) {
if (newUrl.requireAuth && !session.user) {
// User isn’t authenticated
$location.path("/login");
}
});
}
]);