angular-bootstrap 切换按钮如何使用布尔值而不是字符串或整数

angular-bootstrap toggle button how to use boolean instead of string or int

我在 test_page.js

中有一个切换按钮

HTML:

  <button type="button" class="my-button-toggle" ng-model="isTesting"
          btn-checkbox btn-checkbox-true="1" btn-checkbox-false="0">Enable Testing
  </button>

在我的控制器中,我有

$scope.isTesting = parseInt($.cookie(constant.cookies.isTesting))

$scope.$watch('isTesting', function(newValue, oldValue) {
    if (newValue)
        $.cookie(constant.cookies.isTesting, newValue)
    else
        $.removeCookie(constant.cookies.isTesting)
})

我使用此函数将 on/off isTesting 标志存储在 jQuery cookie 中。

现在一切正常。但是我想在 html 中使用 true/false 而不是 1/0 (为了便于阅读)。

我试过btn-checkbox-true="true" btn-checkbox-false="false"$watch回调中求值的newValue是字符串"true"/"false",不是布尔值。

如果您可能会问,我不使用 angular $cookies 因为我网站的某些页面没有使用 angular。 (应该与问题无关。)

删除 btn-checkbox-true="1"btn-checkbox-false="0" 使 newValue 成为 truefalse.

默认值为 true/false,但如果您通过 btn-checkbox-true/false 提供替代值,它们将被视为字符串。