angular-bootstrap toggle按钮如何使用boolean而不是string或int

时间:2015-08-03 13:57:11

标签: angularjs twitter-bootstrap twitter-bootstrap-3 angular-ui

我在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)
})

我使用此函数打开/关闭存储在jQuery cookie中的isTesting标志。

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

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

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

1 个答案:

答案 0 :(得分:4)

删除btn-checkbox-true="1"btn-checkbox-false="0",让newValue成为truefalse

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