【问题标题】:Angular ng-model not updating when checkbox checked with jquery property使用 jquery 属性检查复选框时,Angular ng-model 未更新
【发布时间】:2016-10-31 22:23:11
【问题描述】:

代码试图通过 jquery .prop 函数设置一个选中的属性。 但是 ng-model 还是空白的。

$('document').ready(function(){
    $("#mybutton").on('click',function(){
        $("#mycheckbox").prop('checked',true);
    });
});

<input type="button" id="mybutton" value="click">
<input type="checkbox" id="mycheckbox" name="one" ng-model="arr[1]" value="1" />

{{arr}}   -- is blank when checked with jquery and not blank when manually checked.

当我手动选中复选框时,会绑定 ng-model 并设置其值。 当使用 jQuery 检查复选框时,您能否帮助如何更新 ng-model。

【问题讨论】:

  • 您是否尝试过改用ng-checked="arr[1]"arr 在你的范围内吗?
  • @A.Gille 是的,模型是在我手动设置时设置的。但是当它通过按钮单击触发时,它不会被设置。即通过按钮单击检查复选框时
  • 和上面Gabe的帖子一样,能不能试试触发输入事件。我怀疑你正在做的事情会奏效,除非你有一些 Angular 方法。

标签: javascript jquery angularjs checkbox angular-ngmodel


【解决方案1】:

当 Angular 重新检查 ng model 和 when digest runs 等绑定属性时,您应该阅读。

使用 $scope.$applyAsync() 方法包装你的函数,如下所示:

   $scope.$applyAsync($("#mycheckbox").prop('checked',true));

这应该在控制器内部完成。

如果您不想在无范围路径中执行任何操作,可以使用 $timeout。

还有一次。所有 dom 操作都应该在指令/组件中完成,您可以在其中访问各种角度工具,例如 applyAsync 等。

【讨论】:

    猜你喜欢
    • 2016-03-26
    • 1970-01-01
    • 2015-05-23
    • 1970-01-01
    • 2016-11-04
    • 1970-01-01
    • 2016-10-27
    • 2021-09-25
    • 1970-01-01
    相关资源
    最近更新 更多