LoginSignup
5
3

More than 5 years have passed since last update.

都道府県プルダウンを生成するディレクティブ

Last updated at Posted at 2016-06-02

都道府県マスタがない条件で都道府県プルダウン作るのクソめんどい!!
・・・のでプルダウンを生成するディレクティブ作った。

var app = angular.module('app' []);
app.directive('prefectureList', [function() {
    return {
        restrict    : 'EA',
        require     : '^ngModel',
        replace     : true,
        template    : 
            '<select>'
                +'<option value="">都道府県</option>'
                +'<option ng-repeat="pref in prefectures" value="{[{ pref }]}">{[{ pref }]}</option>'
            +'</select>',
        controller  : function($scope) {
            $scope.prefectures = [
                '北海道', '青森県', '岩手県', '宮城県', '秋田県', '山形県',
                '福島県', '茨城県', '栃木県', '群馬県', '埼玉県', '千葉県',
                '東京都', '神奈県', '新潟県', '富山県', '石川県', '福井県',
                '山梨県', '長野県', '岐阜県', '静岡県', '愛知県', '三重県',
                '滋賀県', '京都府', '大阪府', '兵庫県', '奈良県', '和歌山県',
                '鳥取県', '島根県', '岡山県', '広島県', '山口県', '徳島県',
                '香川県', '愛媛県', '高知県', '福岡県', '佐賀県', '長崎県',
                '熊本県', '大分県', '宮崎県', '鹿児島県', '沖縄県'
            ];
        }
    };   
}]);

/* 
// HTMLにはこう書く (name = selectタグにつけるname値)
<prefecture-list name="prefecture"></prefecture-list>
// restrictには'A'も指定してるので下記でもOKのはず
<div prefecture-list data-name="prefecture"></div>
// (Angularで)必須にしたいならrequiredをつける
<prefecture-list name="prefecture" required></prefecture-list>
*/

valueには都道府県コードを使いたい? 改変してくださいw

5
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
5
3