[英]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-value
和ng-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.