How do I bind to list of checkbox values with AngularJS?

前端 未结 29 2747
天涯浪人
天涯浪人 2020-11-22 05:20

I have a few checkboxes:





        
29条回答
  •  天涯浪人
    2020-11-22 06:15

    Here is the jsFillde link for the same, http://jsfiddle.net/techno2mahi/Lfw96ja6/.

    This uses the directive which is available for download at http://vitalets.github.io/checklist-model/.

    This is the good to have directive as your application will need this functionality much often.

    The code is below:

    HTML:

    Multi Checkbox List Demo


    Selected User Roles

    {{user.roles|json}}

    Provided by techno2Mahi

    JavaScript

    var app = angular.module("app", ["checklist-model"]);
    app.controller('Ctrl1', function($scope) {
      $scope.roles = [
        'guest',
        'user',
        'customer',
        'admin'
      ];
      $scope.user = {
        roles: ['user']
      };
      $scope.checkAll = function() {
        $scope.user.roles = angular.copy($scope.roles);
      };
      $scope.uncheckAll = function() {
        $scope.user.roles = [];
      };
      $scope.checkFirst = function() {
        $scope.user.roles.splice(0, $scope.user.roles.length);
        $scope.user.roles.push('guest');
      };
    });
    

提交回复
热议问题