简体   繁体   English

如何知道如果选中复选框则将元素添加到数组,如果未选中则将其删除?

[英]How to know add element to an array if checkbox is checked and remove it if unchecked?

Here is my code on HTML. 这是我的HTML代码。 I find it on google that I can use this. 我在Google上找到了可以使用的工具。

<input type="checkbox" ng-true-value="addTitle(cpPortfolioItem)" ng-false-value="removeTitle(cpPortfolioItem)">

Here is my angularJS controller. 这是我的angularJS控制器。

$scope.addTitle = function(cpPortfolioItem){
        $scope.selectedTitles.push(cpPortfolioItem.id);
        console.log('$scope.selectedTitles', $scope.selectedTitles);
     };

     $scope.removeTitle = function(cpPortfolioItem){
        $scope.selectedTitles.splice(cpPortfolioItem.id,1);
        console.log('$scope.selectedTitles', $scope.selectedTitles);
     };

it doesn't work. 它不起作用。 I have logged it in console but I can see it neither push or splice the array. 我已经将其记录在控制台中,但是我看不到它既没有推送也没有拼接阵列。 Maybe ng-true-value is not a valid directive? 也许ng-true-value不是有效的指令? Anyone can help me on this? 有人可以帮我吗? I will really appreciate it. 我会很感激的。

Base on the documentation ng-true-value and ng-false-value value are not event handlers. 根据文档 ng-true-valueng-false-value不是事件处理程序。 These are the value set to the model when input is checked( ng-true-value ) or unchecked( ng-false-value ). 这些是在input被选中( ng-true-value )或未选中( ng-false-value )时为model设置ng-false-value

Use this instead or use ng-model and attach $watch to the model. 改用或使用ng-model并将$watch附加到模型。

Use ng-change 使用ng-change

<input type="checkbox" ng-change="titleChange()" ng-model="titleChanged">

In controller, 在控制器中

$scope.titleChanged = false;
$scope.titleChange = function() {
   if ($scope.titleChanged) {
      $scope.addTitle(cpPortfolioItem);
   } else {
      $scope.removeTitle(cpPortfolioItem);
   }
}

Working example:- http://jsfiddle.net/Shital_D/Lcumc3t7/10/ 工作示例: -http : //jsfiddle.net/Shital_D/Lcumc3t7/10/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 选中和取消选中时,将复选框值添加到数组中/从数组中删除复选框值(jQuery) - Add/remove checkbox values to/from array when checked and unchecked (jQuery) 如何向 state 添加/删除数据,具体取决于复选框是否已选中然后未选中 - how to add/remove data to state depending on if the checkbox was checked and then unchecked 如果选中复选框并且未选中复选框而不是删除,如何添加名称属性 - How to add name attribute if checkbox is checked and if it is unchecked than remove 如何根据已选中或未选中复选框添加/删除项目? - How to add/remove item on the basis of checkbox checked or unchecked? 选中复选框时如何将元素添加到数组 - How to add an element to an array when a checkbox is checked 取消选中复选框时,从数组中删除元素 - remove element from array when checkbox unchecked 取消选中/选中复选框时,javascript在div中添加/删除文本 - javascript add/remove text in div when checkbox is unchecked/checked 检查复选框是否被选中增加价值,否则被取消选择价值 - check if checkbox is checked add value else if unchecked remove value GridView中复选框的单击事件,如何知道它是选中还是未选中? - click event of checkbox in gridview, how to know it was checked or unchecked? 如何在选中复选框时将新值推送到数组中,并在使用 Vue.js 取消选中时删除? - How to push new values into array when checkbox is checked and remove when unchecked using Vue.js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM