簡體   English   中英

綁定復選框或多選下拉列表到AngularJS中的數組?

[英]Bind checkbox or multi-select dropdown to array in AngularJS?

我是AngularJS的新手,因此對如何將復選框或多選下拉列表綁定到單獨的列表感到困惑。

<body ng-controller="FooListCtrl">
  <span ng-repeat="foolist in foos">
    <select ng-model="selected" ng-options="foo for foo in foolist" multiple="">
    </select>
  </span>
</body>
'use strict';

function FooListCtrl($scope) {
    $scope.foos = {"Bar": [ "foo", "bar"]};
    $scope.selected = [];
}

FooListCtrl.$inject = ['$scope'];

運行代碼: http : //jsfiddle.net/gBcN2/

如果我說對了你想要什么:

  1. 您沒有ng-app定義。
  2. 如果您將AngularJS用作外部資源,則No wrap - in <head> AngularJS片段的jsFiddle上,將No wrap - in <head> wrap-置於No wrap - in <head>加載模式下。
  3. selected模型具有其自己的“范圍”,因為您使用ng-repeat 明白我的意思,這是您的代碼的固定版本: http : //jsfiddle.net/gBcN2/2/

首先{{selected}}可以正常工作,但是第二個是ng-repeat范圍的“外部”。

PS:
如果要像在示例中編寫的那樣使用ng-repeat則不必使用ng-repeat快速了解我的操作方式。

編輯:
對於復選框,它類似於-http://jsfiddle.net/qQg8u/2/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM