Angularjs材质全部选中并全部取消选中不起作用?

时间:2019-01-03 12:18:53

标签: angularjs angular-material

我用Slectall和Deselect all选项创建了Multi select下拉列表。 在这里,我有所有复选框,可以选择全部并取消选择所有复选框,但是它仅在工作一次之后才起作用,有人可以在下面的代码中帮助我。

<md-select ng-model="selectedUser" multiple="" ng-model-options="{trackBy: '$value'}">
    <md-optgroup >
        <md-option ng-value="" ng-click="doSelectedUser()">all</md-option>
        <md-option ng-value="user" ng-repeat="user in newusers">{{ user}}</md-option>
    </md-optgroup>
</md-select>

下面是我的控制器

var myApp = angular.module('sandbox', ['ngMaterial']);

myApp.controller('myCtrl', function($scope) {

 $scope.selectedUser =[];
 $scope.newusers = ['A','B','c']

 $scope.doSelectedUser = function () {
 if($scope.selectedUser.length > 0)
 {
    $scope.selectedUser = [];
} else {
    $scope.selectedUser = $scope.newusers; }
}

});

link

1 个答案:

答案 0 :(得分:0)

您必须与此

<md-select ng-model="selectedUser" multiple="" ng-model-options="{trackBy: 
'$value'}">
 <md-optgroup>
 <md-button layout-fill  ng-click="doSelectedUser()">
 {{(selectedUser.length == newusers.length)?'Deselect All':'Select All'}}
 </md-button>
 </md-checkbox>
 <md-option ng-value="user" ng-repeat="user in newusers" >{{ user}}</md-option>
 </md-optgroup>
</md-select>

 $scope.selectedUser = [];
 $scope.newusers = ['A', 'B', 'c']
 $scope.doSelectedUser = function () { 
 if ($scope.selectedUser.length == $scope.newusers.length) {
 $scope.selectedUser = [];
 } else {
 $scope.selectedUser = $scope.newusers;
 }
 }