Angularjs-views

提供:Dev Guides
移動先:案内検索

AngularJS-ビュー

AngularJSは、単一ページ上の複数のビューを介した単一ページアプリケーションをサポートします。 これを行うために、AngularJSはng-viewおよびng-templateディレクティブ、および$ routeProviderサービスを提供しています。

ng-viewディレクティブ

ng-viewディレクティブは、構成に基づいて対応するビュー(HTMLまたはng-templateビュー)を配置できるプレースホルダーを作成するだけです。

使用法

メインモジュール内でng-viewを使用してdivを定義します。

<div ng-app = "mainApp">
   ...
   <div ng-view></div>

</div>

ng-templateディレクティブ

ng-templateディレクティブは、scriptタグを使用してHTMLビューを作成するために使用されます。 ビューにコントローラーをマップするために$ routeProviderによって使用される_id_属性が含まれています。

使用法

メインモジュール内でタイプがng-templateのスクリプトブロックを定義します。

<div ng-app = "mainApp">
   ...

   <script type = "text/ng-template" id = "addStudent">
      <h2> Add Student </h2>
      {{message}}
   </script>

</div>

$ routeProviderサービス

$ routeProviderは、URLの構成を設定し、それらを対応するHTMLページまたはng-templateにマップし、同じものを使用してコントローラーを接続する主要なサービスです。

使い方1

メインモジュール内でタイプがng-templateのスクリプトブロックを定義します。

<div ng-app = "mainApp">
   ...
   <script type = "text/ng-template" id = "addStudent">
      <h2> Add Student </h2>
      {{message}}
   </script>
</div>

使い方2

メインモジュールでスクリプトブロックを定義し、ルーティング構成を設定します。

var mainApp = angular.module("mainApp", ['ngRoute']);

mainApp.config(['$routeProvider', function($routeProvider) {
   $routeProvider

   .when('/addStudent', {
      templateUrl: 'addStudent', controller: 'AddStudentController'
   })
   .when('/viewStudents', {
      templateUrl: 'viewStudents', controller: 'ViewStudentsController'
   })
   .otherwise ({
      redirectTo: '/addStudent'
   });

}]);

上記の例では、次の点を考慮することが重要です-

  • $ routeProviderは、 '$ routeProvider’としてキーを使用して、mainAppモジュールのconfigの下の関数として定義されます。
  • $ routeProvider.whenは、「addStudent」にマップされる「/addStudent」というURLを定義します。 addStudentはメインHTMLページと同じパスに存在する必要があります。 HTMLページが定義されていない場合は、ng-templateをid = "addStudent"とともに使用する必要があります。 ng-templateを使用しました。
  • 「それ以外」は、デフォルトビューを設定するために使用されます。
  • 「コントローラー」は、ビューに対応するコントローラーを設定するために使用されます。

次の例は、上記のすべてのディレクティブの使用方法を示しています。

testAngularJS

<html>
   <head>
      <title>Angular JS Views</title>
      <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
      <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular-route.min.js">
      </script>
   </head>

   <body>
      <h2>AngularJS Sample Application</h2>
      <div ng-app = "mainApp">
         <p><a href = "#addStudent">Add Student</a></p>
         <p><a href = "#viewStudents">View Students</a></p>
         <div ng-view></div>

         <script type = "text/ng-template" id = "addStudent">
            <h2> Add Student </h2>
            {{message}}
         </script>

         <script type = "text/ng-template" id = "viewStudents">
            <h2> View Students </h2>
            {{message}}
         </script>
      </div>

      <script>
         var mainApp = angular.module("mainApp", ['ngRoute']);
         mainApp.config(['$routeProvider', function($routeProvider) {
            $routeProvider

            .when('/addStudent', {
               templateUrl: 'addStudent',
               controller: 'AddStudentController'
            })
            .when('/viewStudents', {
               templateUrl: 'viewStudents',
               controller: 'ViewStudentsController'
            })
            .otherwise({
               redirectTo: '/addStudent'
            });
         }]);
         mainApp.controller('AddStudentController', function($scope) {
            $scope.message = "This page will be used to display add student form";
         });
         mainApp.controller('ViewStudentsController', function($scope) {
            $scope.message = "This page will be used to display all the students";
         });
      </script>

   </body>
</html>

出力

ファイル_testAngularJS_をWebブラウザーで開き、結果を確認します。