【问题标题】:checkbox directive using angularjs使用 angularjs 的复选框指令
【发布时间】:2016-02-27 10:46:49
【问题描述】:

我正在尝试使用 angularjs.The code is here in JSFIDDLE 创建一个复选框指令

它只适用于 ng-model 和文本。

但是如果我添加 ng-true-value 和 ng-false 值,它会抛出错误

nged2angular.js:12520 Error: [ngModel:constexpr] Expected constant expression for `ngTrueValue`, but saw `bindedTrueValue`.

我还需要在指令中使用 ng-change 控制器功能。

【问题讨论】:

    标签: angularjs checkbox directive


    【解决方案1】:

    两件事,

    首先,您将 'checkboxChanged' 函数传递给指令,因此您需要在指令范围内使用 &ngChange 而不是 =ngChange。

    如果没有 {{}},则 bindedTrueValue 和 bindedFalseValue 将被视为字符串,而不是来自控制器的角度变量。所以,应该改成

    <input type="checkbox" ng-model="bindedModel" ng-true-value="{{bindedTrueValue}}" ng-false-value="{{bindedFalseValue}}" ng-change="bindedChange">
    

    查看此codepen 以获得工作演示。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-22
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多