选择加载时的复选框

时间:2017-02-24 10:38:14

标签: javascript angularjs

从数据库加载所有主题后,我需要为特定用户选择一个。我怎么能这样做?

<div ng-repeat="item in topicEditItems">
    <md-checkbox ng-checked="existsEditTopic(item, topicEditSelected);" ng-click="toggleEditTopic(item, topicEditSelected);" value="{{item.TopicID}}">
        {{item.TopicName}}
    </md-checkbox>
</div>

$scope.topicEditSelected = [];

adminService.getTopics().then(function (response) {
    $scope.topicEditItems = $.parseJSON(response.data);
    //Select topics
})

$scope.toggleEditTopic = function (item, list) {
    var idx = list.indexOf(item);
    if (idx > -1) {
        list.splice(idx, 1);
    }
    else {
        list.push(item);
    }
};

$scope.existsEditTopic = function (item, list) {
    return list.indexOf(item) > -1;
};

2 个答案:

答案 0 :(得分:0)

$scope.existsEditTopic更新为

  $scope.existsEditTopic = function (item, list) {
     var index = list.indexOf(item);
     if(index > -1){
        return true;
     }else{
        return false;                
      }
};

答案 1 :(得分:0)

最简单的方法是将2个复选框模仿原始1,然后根据您的过滤器显示和隐藏。您可能需要将md-checkbox样式应用于假的

<input type="checkbox" class="check"  aria-label="Cb user" 
   ng-model="cbFake"  ng-if="isTopicExist(item.id)" ng-checked="isTopicExist(item.id)"/> 

<md-checkbox ng-checked="exists(item, selected)" ng-click="toggle(item, selected)" aria-label="Cb user" 
  ng-model="cbOri"  ng-if="!isTopicExist(item.id)"> </md-checkbox>