Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
1
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

@sugasaki

AngularJS Checkbox群から選択したものを取得

AngularJSで、Checkbox群の中から選択したものだけを保持するオブジェクトを提供する。

使い方

モジュール「"checklist-model"」を読み込む

js

var app = angular.module("app", ["checklist-model"]);
app.controller('Ctrl1', function($scope) {

//Checkbox表示用リストの作成
  $scope.roles = [
    'guest', 
    'user', 
    'customer', 
    'admin'
  ];

  //状態保存用オブジェクト作成
  $scope.user = {
    roles: ['user']
  };

});


html


<label ng-repeat="role in roles">
  <input type="checkbox" checklist-model="user.roles" checklist-value="role"> {{role}}
</label>



選択項目の確認用リスト表示
<div class="col-lg-2">
    <ul class="list-group">
        <li class="list-group-item" ng-repeat="item in user.roles">{{item.Text}}</li>
    </ul>
</div>

拡張

Jsonをそのまま格納も可能
その際にはchecklist-value → data-checklist-valueに変更する

<label ng-repeat="role in roles">
  <input type="checkbox" checklist-model="user.roles" checklist-value="role"> {{role}}
</label>
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
1
Help us understand the problem. What are the problem?