ngModel:我的 angularjs 应用程序中的 constexpr 错误,但 jsfiddle 上的相同代码很好

ngModel : constexpr error in my angularjs app, but same code on jsfiddle is good

我在 my app 中使用此代码出现了一个非常令人惊讶的错误:

<div class="ui toggle checkbox">
    <input type="checkbox" ng-model="pimp.init.carte.choice_1" ng-checked="pimp.init.carte.choice_1 == 'online'" ng-true-value="online" ng-false-value="offline" />
    <label>OSM 
        <span ng-show="pimp.init.carte.choice_1 == 'online'">en ligne</span>
        <span ng-show="pimp.init.carte.choice_1 == 'offline'">hors ligne</span>
    </label>{{pimp.init.carte.choice_1}}
</div>

在控制器中,pimp.init.carte.choice_1默认设置为'online'。

我有这个错误:

ngModel:constexpr] http://errors.angularjs.org/1.3.11/ngModel/constexpr?p0=ngTrueValue&p1=online

我试过 repeat the behavior in a jsfiddle 但这里似乎没问题,所以我不明白为什么它在我的应用程序中不起作用。

怎么了?

您在您的应用中使用 Angular 1.3.11,在您的示例中使用 1.2.1

您收到错误是因为 1.3.0-beta.15 中引入了以下更改:

ngTrueValue and ngFalseValue now support parsed expressions which the parser determines to be constant values.

BREAKING CHANGE:

Previously, these attributes would always be treated as strings. However, they are now parsed as expressions, and will throw if an expression is non-constant.

To convert non-constant strings into constant expressions, simply wrap them in an extra pair of quotes, like so:

<input type="checkbox" ng-model="..." ng-true-value="'truthyValue'">

在您的情况下,更改以下内容:

ng-true-value="online" ng-false-value="offline"

收件人:

ng-true-value="'online'" ng-false-value="'offline'"

演示: http://plnkr.co/edit/yvTlKwa0NlWZoyWXWVXH?p=preview