我正在创建一个网络应用程序,其中我使用切换按钮,当用户单击切换按钮时'yes'
应该存储在$scope
变量中如果用户点击两次'no'
应该存储在$scope
变量中
这是我的切换按钮编码
<div class="switch">
<input ng-click="clickcheckbox()" id="cmn-toggle-1" class="cmn-toggle cmn-toggle-round" type="checkbox">
<label for="cmn-toggle-1"></label>
</div>
我已经采取了ng-click
,因为我无法使用ng-checked
来做到这一点
这是我的 Controller
$scope.clickcheckbox=function(){
//if user check the checkbox
$scope.check='yes';
//if user uncheck the checkbox
$scope.check='no';
}
如果还有其他更好的方法可以做到这一点(请帮助我)
最佳答案
您可以使用以下代码:
HTML
<input type="checkbox" ng-model="checkboxModel.value2"
ng-true-value="'YES'" ng-false-value="'NO'">
JavaScript:
$scope.checkboxModel = {
value2 : 'YES'
};
地点:
ngTrueValue : The value to which the expression should be set when selected.
ngFalseValue : The value to which the expression should be set when not selected.
关于javascript - 使用 angularjs 在复选框选中/取消选中时打印不同的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41222592/