这是我在 HTML 上的代码。我在谷歌上找到它,我可以使用它。
<input type="checkbox" ng-true-value="addTitle(cpPortfolioItem)" ng-false-value="removeTitle(cpPortfolioItem)">
这是我的 angularJS Controller 。
$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);
};
这是行不通的。我已经在控制台中记录了它,但我看不到它既没有推送也没有拼接数组。也许 ng-true-value
不是一个有效的指令?任何人都可以帮助我吗?我将非常感激。
最佳答案
基于documentation ng-true-value
和 ng-false-value
值不是事件处理程序。这些是当 input
被选中(ng-true-value
)或未被选中(ng-false-值
).
使用this或者使用 ng-model
并将 $watch
附加到模型。
关于javascript - 如果复选框被选中,如何知道将元素添加到数组中,如果未选中,如何将其删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30184267/