【问题标题】:Angularjs: checkbox and ng-changeAngularjs:复选框和 ng-change
【发布时间】:2020-11-17 14:45:05
【问题描述】:

我很难理解 ng-change 的工作原理。我有一个要邀请参加拍卖的用户列表。我想用一个复选框来做到这一点。如果用户被选中,他的名字必须被保存到一个数组中。稍后我会邀请他们(我只知道该怎么做)。但我不明白如何使用复选框。 我做了这样的事情:

<ul class="list-group" ng-repeat="user in users">
    <li class="list-group-item" ng-hide="user.name == profile">
        <img ng-src="{{user.img}}" class="image2" >
        <div class="username"> {{user.name}}</div>
        <div class="userrole"> {{user.role}} </div>
        <div class="usercompany">{{user.company}}</div>
        <input type="checkbox"  ng-model="isChecked" ng-change="insertinvited(user.name)">
    </li>
</ul>

在我的控制器中:

$scope.invited = [];
$scope.insertinvited= function (name) {
    if($scope.isChecked){
        $scope.invited.push(name)
    } else {
        console.log($scope.invited);
    }
};

但这不起作用,在控制台中数组始终为空。

【问题讨论】:

    标签: javascript angularjs checkbox angularjs-ng-change


    【解决方案1】:

    您的代码中的问题是您的复选框模型 (isChecked) 被所有 users 使用您 ngRepeated on:

    <ul class="list-group" ng-repeat="user in users">
        ...
        <input type="checkbox" ng-model="isChecked" ng-change="insertinvited(user.name)">
    </ul>
    

    我建议您为每个用户创建一个复选框模型:

    <input type="checkbox" ng-model="user.isChecked" ng-change="insertinvited(user)">
    

    请注意,在您的 ng-change 中,我传递了整个 user 对象,而不仅仅是 user.name(因为我还需要 user.isChecked 属性)。


    这里是“新”insertinvited() 函数:

    $scope.insertinvited = function(user) {
        if(user.isChecked) {
            $scope.invited.push(user.name);
        } else {
            var toDel = $scope.invited.indexOf(user.name);
            $scope.invited.splice(toDel, 1);
        }
    }
    

    【讨论】:

    • 好的,这行得通!但是,如果我再次单击时必须从数组中删除名称怎么办?在此代码中,如果我单击复选框,则会保存用户名。但是,如果我取消选中该复选框,则名称仍保留在数组中。如果我想在取消选中复选框时删除名称,我必须使用什么?
    • @MarcoPagano 我已经更新了我的答案,添加了一个 else 进行删除。如果它解决了您的问题,请接受答案
    • 等等,我不知道我更喜欢哪个答案:D。但是,其他不起作用。当我只选择一个用户时,它可以工作。但是当我选择了多个时,它会拼接我添加的最后一个,而不是正确的
    • 使用 $scope.invited.splice(toDel, 1);然后它会工作
    【解决方案2】:

    你需要的是isChecked 用于ng-repeat 中的所有项目,而不仅仅是单个变量。所以,我把你的checkbox 改成了:

    <input type="checkbox" ng-model="user.isChecked" ng-change="insertinvited(user)">
    

    以下是工作示例:

    angular.module("myApp", [])
      .controller("myCtrl", function($scope) {
        $scope.users = [{
          name: "test1",
          role: "manager",
          company: "google",
          img: ""
        }];
    
        $scope.invited = [];
        $scope.insertinvited = function(user) {
          if (user.isChecked) {
            $scope.invited.push(user.name)
          } 
          console.log($scope.invited);
    
        };
      })
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <div ng-app="myApp" ng-controller="myCtrl">
      <ul class="list-group" ng-repeat="user in users">
        <li class="list-group-item" ng-hide="user.name == profile">
          <img ng-src="{{user.img}}" class="image2">
          <div class="username"> {{user.name}}</div>
          <div class="userrole"> {{user.role}} </div>
          <div class="usercompany">{{user.company}}</div>
          <input type="checkbox" ng-model="user.isChecked" ng-change="insertinvited(user)">
        </li>
      </ul>
    </div>

    编辑:但是,这样做,您还需要处理删除。我建议您改用以下方法。

    说明:在更改任何复选框值时,您过滤所有将isChecked 设置为true 的用户。

    angular.module("myApp", [])
      .controller("myCtrl", function($scope) {
        $scope.users = [{
          name: "test1",
          role: "manager",
          company: "google",
          img: ""
        }];
    
        $scope.invited = [];
        $scope.insertinvited = function() {
          $scope.invited = $scope.users.filter(obj => obj.isChecked)
          $scope.invited = $scope.invited.map(obj => obj.name)
          console.log($scope.invited);
    
        };
      })
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <div ng-app="myApp" ng-controller="myCtrl">
      <ul class="list-group" ng-repeat="user in users">
        <li class="list-group-item" ng-hide="user.name == profile">
          <img ng-src="{{user.img}}" class="image2">
          <div class="username"> {{user.name}}</div>
          <div class="userrole"> {{user.role}} </div>
          <div class="usercompany">{{user.company}}</div>
          <input type="checkbox" ng-model="user.isChecked" ng-change="insertinvited(user)">
        </li>
      </ul>
    </div>

    【讨论】:

    • 好的,这行得通!但是,如果我再次单击时必须从数组中删除名称怎么办?在此代码中,如果我单击复选框,则会保存用户名。但是,如果我取消选中该复选框,则名称仍保留在数组中。如果我想在取消选中复选框时删除名称,我必须使用什么?
    【解决方案3】:

    使用它

    <input type="checkbox" id="{{'j' + $index}}" name="{{'j' + $index}}" ng-model="tasks.Checked" ng-change="checkinTask(tasks)"
    

    这部分来自控制器。

    $scope.checkinTask = function (task) {
    
                            if (task.Checked === true) {
                                task.Lineclass = "checkedTask";
                                $scope.disabled = false;
                                trash.removeClass("disabled");
                            } else {
                                task.Lineclass = "";
                            }
                            var checkedExist = false;
                            tasks.forEach(function (v) {
                                if (v.Checked) {
                                    checkedExist = true;
                                }
                            });
                            if (!checkedExist) {
                                $scope.disabled = true;
                                trash.addClass("disabled");
                            }
                        };
    

    【讨论】:

      猜你喜欢
      • 2015-05-20
      • 1970-01-01
      • 1970-01-01
      • 2016-10-08
      • 1970-01-01
      • 1970-01-01
      • 2015-08-29
      • 1970-01-01
      • 2014-09-07
      相关资源
      最近更新 更多