angular checkbox post 可以是带有 ng-model 的布尔值,而不是字符串吗?

Can angular checkbox post a boolean with ng-model, not a string?

以下代码在选中时将给出 support_enable='true',如果未选中则返回 null。

<form>
  <input type="checkbox" name="support_enable" ng-value="config.support_enable" ng-model="config.support_enable">
</form>

以下代码在选中时将给出 support_enabled='on',如果未选中则返回 null。

<form>
  <input type="checkbox" name="support_enable" ng-model="config.support_enable">
</form>

我想知道有没有办法post一个布尔值?

用老方法做,在复选框插入隐藏字段之前

<form>
<input type="hidden" name="support_enable" ng-value="config.support_enable" ng-model="config.support_enable">
<input type="checkbox" name="support_enable" ng-model="config.support_enable">
</form>

尝试在ng-change上调用函数并根据获取的值更新变量值布尔值

// in your controller

$scope.checkValue = function(value){
  if(value == 'on'){
    config.support_enable = true;
  }
  else 
    config.support_enable = false;
}
<form>
  <input type="checkbox" name="support_enable" ng-model="config.support_enable" ng-change="checkValue(config.support_enable)">
</form>