「労働者が自分の仕事をうまくやりたいなら、まず自分の道具を研ぎ澄まさなければなりません。」 - 孔子、「論語。陸霊公」
表紙 > プログラミング > AngularJSマルチチェックボックス値は、メソッドをリストするためにバインドされています

AngularJSマルチチェックボックス値は、メソッドをリストするためにバインドされています

2025-04-14に投稿されました
ブラウズ:208

How to Bind Multiple Checkbox Values to a List in AngularJS?

angularjs?ただし、AngularJSのNG-Modelプロパティは、単一のチェックボックス値をバインドすることに限定されているようです。この記事では、この問題を解決するための2つのアプローチを検討し、簡単な配列またはオブジェクトの配列を入力データとして活用します。 -1" ng-click = "toggleSelection(furtname)" > {{furtname}}

controller:

app.controller( 'simplearrayctrl'、['$ scope'、function simplearrayctrl($ scope){ //果物 $ SCOPE.FRUITS = ['Apple'、 'Orange'、 'pear'、 'naartjie']; //選択された果物 $ scope.selection = ['apple'、 'pear']; //選択の切り替え $ scope.toggreSelection = function toggleSelection(furtname){ var idx = $ scope.selection.indexof(fruitname); if(idx> -1){ $ SCOPE.SELECTION.SPLICE(IDX、1); } それ以外 { $ SCOPE.SELECTION.PUSH(FruitName); } }; }]);

pro:

simpleデータ構造

name &&&&]
cons:

cumbersom Operations

アプローチ2:オブジェクトアレイ

html:
  • {{furt.name}}
controller:

app.controller( 'objectArrayctrl'、['$ scope'、 'filterfilter'、function objectarrayctrl($ scope、filterfilter){ //果物 $ scope.fruits = [ {name: 'apple'、selected:true}、 {name: 'orange'、selected:false}、 {name: 'pear'、selected:true}、 {name: 'naartjie'、selected:false} ]; //選択された果物 $ scope.selection = []; //選択した果物のヘルパー $ scope.selectedFruits = function selectedFruits(){ return filterfilter($ scope.fruits、{selected:true}); }; //変更のために果物を見てください $ scope。$ watch( 'fruits | filter:{selected:true}'、function(nv){ $ scope.selection = nv.map(function(fruic){ return furt.name; }); }、 真実); }]);

  • プロ:

cons:

cumbersome name

app.controller('ObjectArrayCtrl', ['$scope', 'filterFilter', function ObjectArrayCtrl($scope, filterFilter) {

  // Fruits
  $scope.fruits = [
    { name: 'apple',    selected: true },
    { name: 'orange',   selected: false },
    { name: 'pear',     selected: true },
    { name: 'naartjie', selected: false }
  ];

  // Selected fruits
  $scope.selection = [];

  // Helper for selected fruits
  $scope.selectedFruits = function selectedFruits() {
    return filterFilter($scope.fruits, { selected: true });
  };

  // Watch fruits for changes
  $scope.$watch('fruits|filter:{selected:true}', function (nv) {
    $scope.selection = nv.map(function (fruit) {
      return fruit.name;
    });
  }, true);
}]);
http://jsbin.com/imaquc/1/

]

最新のチュートリアル もっと>

免責事項: 提供されるすべてのリソースの一部はインターネットからのものです。お客様の著作権またはその他の権利および利益の侵害がある場合は、詳細な理由を説明し、著作権または権利および利益の証拠を提出して、電子メール [email protected] に送信してください。 できるだけ早く対応させていただきます。

Copyright© 2022 湘ICP备2022001581号-3