跟踪多个选择框项目

时间:2015-05-28 13:57:25

标签: angularjs

我有以下内容:

http://jsfiddle.net/Qgmz7/119/

我想跟踪所选内容和不选内容,我该怎么做?

<div ng-app>
    <div ng-controller="cCtrl">
        <select multiple ng-model="campaign" ng-options="c.ID as c.Name for c in campaigns">
            <option ng-selected="c.Selected" value="">-- choose campaign --</option>
        </select>
        <ul>
            <li ng-repeat="item in campaigns">{{item.Name}} - {{item.Selected}}</li>
        </ul>
    </div>
</div>

function cCtrl($scope) {    
    $scope.campaigns = [
        {Selected:true, ID: 1, Name:"James"},
        {Selected:false, ID: 2, Name:"James"},
        {Selected:true, ID: 3, Name:"James"},
    ];
}

1 个答案:

答案 0 :(得分:1)

您好试试这个http://jsfiddle.net/a63k106s/

function cCtrl($scope) {  
    $scope.campaigns = [
        {Selected:true, ID: 4, Name:"James"},
        {Selected:false, ID: 2, Name:"James"},
        {Selected:true, ID: 3, Name:"James"},
    ];

    $scope.$watch('campaign', function (nowSelected) {          
        angular.forEach($scope.campaigns, function(campaign) {
            campaign.Selected = nowSelected.indexOf(campaign.ID) >= 0
        });
    });
}