我在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
,因为我的网站的某些页面没有使用角度。 (这应与问题无关。)
答案 0 :(得分:4)
删除btn-checkbox-true="1"
和btn-checkbox-false="0"
,让newValue
成为true
或false
。
默认值为true
/ false
,但如果您通过btn-checkbox-true/false
提供替代值,则会将其视为字符串。