AngularJS指令-如何将$attrs[“value”转换为布尔值
我正在使用我在网上找到的指令:AngularJS指令-如何将$attrs[“value”转换为布尔值,angularjs,angularjs-directive,Angularjs,Angularjs Directive,我正在使用我在网上找到的指令: <input icheck type="radio" ng-model="vm.isEmployed" value="true" /> Yes <input icheck type="radio" ng-model="vm.isEmployed" value="false" /> No 我面临的问题是,$attrs[“value”]始终是一个字符串,但我的ngModel中的属性是一个布尔值 我尝试了eval和Boolean。但我相信这更多
<input icheck type="radio" ng-model="vm.isEmployed" value="true" /> Yes
<input icheck type="radio" ng-model="vm.isEmployed" value="false" /> No
我面临的问题是,$attrs[“value”]
始终是一个字符串
,但我的ngModel中的属性是一个布尔值
我尝试了
eval
和Boolean
。但我相信这更多的是一个与AngularJS指令相关的问题。非常感谢您的帮助。您可以评估属性:
value = $scope.$eval($attrs.value);
您可以将truthy/falsy值添加到标记中以澄清问题--
解决方案是使用
ng值
而不是值
。从以下地址的文档:
如果您需要一个
非字符串模型(布尔、数组等)
因此,我将视图更改为:
<input icheck type="radio" ng-model="vm.isEmployed" ng-value="true" /> Yes
<input icheck type="radio" ng-model="vm.isEmployed" ng-value="false" /> No
是
不
如果你已经“尝试了很多方法”,为什么不在你的问题中分享它们,这样想回答的人就不会想到你已经尝试过的东西?完成了。谢谢你的建议。为什么$attrs['value']='true'
不起作用呢?我找到了正确的方法,但你的答案很有见地。谢谢
ng-true-value="'true'" ng-false-value="'false'"
<input icheck type="radio" ng-model="vm.isEmployed" ng-value="true" /> Yes
<input icheck type="radio" ng-model="vm.isEmployed" ng-value="false" /> No