【问题标题】:How to get the updated model after change the checkbox state using the ng-checked [duplicate]使用 ng-checked [重复] 更改复选框状态后如何获取更新的模型
【发布时间】:2018-07-06 15:27:02
【问题描述】:

我有一定的检查复选框的条件,我正在使用条件运算符的 ng-checked 中的功能逻辑。 这对我来说很好。 问题是,在提交模型值时,我没有得到 ng-checked 值。 如何也修改经过 ng 检查的值?希望我能得到答案。 这是我试过的:

<input type="checkbox" id="View" ng-model="module.View"
       ng-checked="((findViewCheckBoxIsChecked(module)) || (module.ViewAll))"
       ng-disabled="disableCheckBox(module)">
$scope.findViewCheckBoxIsChecked = function (module) {    
      if (module.View || module.ViewAll || module.Name == 'x' && module.Add
            || module.Name == 'x' && module.Edit
            || module.Name == 'x' && module.Delete
            || module.Name == 'x' && module.Add
            || module.Name == 'y' && module.Edit
            || module.Name == 'y' && module.Delete
             || module.Name == 'z' && module.Add
            || module.Name == 'z' && module.Edit
            || module.Name == 'z' && module.Delete) {
            return true;
        }
        else {
            return false;
        }
    }

【问题讨论】:

  • 我不认为我理解这个问题。另外,如果可能,请发布findViewCheckBoxIsChecked 的代码。
  • 是的,我加了兄弟
  • 你现在收到了吗?
  • 嗯,我在那里看到了很多条件。老实说,我们应该在上下文中看到这一点,例如,当问题出现时,module 有什么价值?如果你能创建一个Minimal, Complete, and Verifiable example ... 那会很有帮助。
  • moudle 是模型对象兄弟。如果我单击一个复选框意味着另一个复选框也将被选中,如果其他代码。问题是第二个选中的复选框在提交后没有反映在模型对象中。这是我的问题兄弟。

标签: angularjs


【解决方案1】:

documentation for ng-checked 明确指出不应与ng-model 一起使用。

来自文档:

ngChecked

如果ngChecked 中的表达式为真,则设置元素的选中属性。

注意此指令不应与ngModel 一起使用,因为这会导致意外行为。

— AngularJS ng-checked Directive API Reference


更新

哦...我不知道...那么如何设置我必须检查的复选框的值?有什么想法吗?

可以使用ng-change 指令来计算兄弟复选框的值:

<input type="checkbox" id="ViewAll" ng-model="module.ViewAll"
       ng-change="updateCheckboxes(module)" />

<input type="checkbox" id="View" ng-model="module.View"
       ̶n̶g̶-̶c̶h̶e̶c̶k̶e̶d̶=̶"̶(̶(̶f̶i̶n̶d̶V̶i̶e̶w̶C̶h̶e̶c̶k̶B̶o̶x̶I̶s̶C̶h̶e̶c̶k̶e̶d̶(̶m̶o̶d̶u̶l̶e̶)̶)̶ ̶|̶|̶ ̶(̶m̶o̶d̶u̶l̶e̶.̶V̶i̶e̶w̶A̶l̶l̶)̶)̶"̶
       ̶n̶g̶-̶d̶i̶s̶a̶b̶l̶e̶d̶=̶ ̶"̶d̶i̶s̶a̶b̶l̶e̶C̶h̶e̶c̶k̶B̶o̶x̶(̶m̶o̶d̶u̶l̶e̶)̶"̶ 
       ng-disabled= "disableViewCheckBox"
       ng-change="updateCheckboxes(module)" />
$scope.updateCheckboxes = function(module) {
    module.View = (($scope.findViewCheckBoxIsChecked(module)) || (module.ViewAll));
    $scope.disableViewCheckbox = $scope.disableCheckBox(module);
};

出于性能原因,应避免将函数放入 AngularJS 表达式中。框架在每个摘要循环中调用一次或多次。通过使用ng-change 指令,这些函数仅在用户更改输入时才被调用。

当然,ng-change 函数只在用户输入时调用。控制器在分配新模型值时还应调用$scope.updateCheckboxes 函数。

【讨论】:

  • 哦...我不知道这个...那么如何设置我必须检查的复选框的值?有什么想法吗?
  • 没听懂你能不能详细点我听不懂
猜你喜欢
  • 2015-10-03
  • 1970-01-01
  • 2018-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多