这是我的代码:
<input type="checkbox" ng-model="variable">
在这种情况下variable将true或false根据复选框的状态。我需要设置 not true/ falsebut "+"/ "-"。最简单的方法是什么?谢谢。
这是我的代码:
<input type="checkbox" ng-model="variable">
在这种情况下variable将true或false根据复选框的状态。我需要设置 not true/ falsebut "+"/ "-"。最简单的方法是什么?谢谢。
根据文档,您应该使用ng-true-value和/或ng-false-value覆盖true和false值:
<input type="checkbox" ng-model="variable" ng-true-value="+" ng-false-value="-">
使用 ng-click 来做到这一点。
<input type="checkbox" ng-model="variable" ng-click="check()">
在控制器中:
$scope.check = function() {
$scope.selected = $scope.variable === true ? '+' : '-';
};
只需在您的复选框模型上设置一个手表。
HTML:
<input type="checkbox" ng-model="isChecked">
JavaScript:
// set initial value
$scope.isChecked = $scope.variable === '+';
// watch for changes to the checkbox model
$scope.$watch('isChecked', function(newVal) {
$scope.variable = newVal ? '+' : '-';
};