【发布时间】:2015-05-14 10:41:27
【问题描述】:
我的复选框输入根据变量trueVal(一个字符串)动态设置“真”值。
<input ng-model="foo" ng-true-value="'{{trueVal}}'">
例如,对于trueVal = "something",选中复选框时为foo === "something"。
这有效,除非trueVal 等于其中包含单引号的字符串:Customer didn't understand。在这种情况下,有一个错误:
错误:[$parse:lexerr] 词法分析器错误:表达式 ['客户不理解'] 中第 27-28 列 ['] 处的未终止引号。
我不能去掉单引号,因为字符串应该按原样设置为foo。
更广泛的背景:
我正在根据从后端获得的options 列表创建一组复选框:
<div ng-repeat="(key,option) in options">
<input type="checkbox"
ng-model="foo[key]"
ng-true-value="'{{option.trueVal}}'"
ng-false-value="null">
</div>
所以,给定:
options = {
0: {trueVal: "Customer was late"},
1: {trueVal: "Customer didn't understand"}
};
我希望看到:
foo = {
1: "Customer didn't understand"
};
如果第二个框被选中。
【问题讨论】:
标签: angularjs