简体   繁体   English

角度:返回API的值后,直到我提交表单后,表单上的复选框才被选中

[英]ANGULAR: checkbox on form does not become checked when value from API is returned until I submit the form

I've been stuck on what seems like a very simple problem for the last hour and a half now and any help would be infinitely appreciated because it is driving me insane. 在过去的一个半小时里,我一直被困在一个看似非常简单的问题上,任何帮助将无穷无尽,因为它使我发疯。

I have a checkbox on a form that I want to appear as checked when the its model that's returned from the API is equal to 1, and not checked when the value is 0. I'm using a very simple checkbox for it. 我在表单上有一个复选框,当从API返回的其模型等于1时,我希望显示为选中状态;而当值为0时,则不选中该复选框。

The checkbox , however is not checked when I load the form and the API returns a value of 1. When I click on the button that submits the entire form, however, the checkbox THEN becomes checked. 但是,当我加载表单时,不会选中checkbox ,并且API返回的值为1。但是,当我单击提交整个表单的按钮时, checkbox THEN就被选中了。 If I reload the page, the checkbox goes back to being unchecked. 如果我重新加载页面,则该复选框会回到未选中状态。 Is there something I'm missing here? 我在这里想念什么吗?

Here is the HTML: 这是HTML:

<div class="form-group">
    <label class="col-sm-2 control-label">Make Payment Optional</label>
    <div class="col-sm-4 center-checkbox">
        <input type="checkbox"class="center-checkbox" ng-model="formData.paymentMode" ng-true-value="1" ng-false-value="0" validate-servererror="paymentMode"/>
    </div>
</div>

The code is working just fine in this CodePen . 该代码在此CodePen中正常工作。 I'm setting in the controller the model: 我在控制器中设置模型:

  $scope.formData = {
    paymentMode : 1
  }

Actually sounds like a problem with the digest cycle, as if you were assigning the model somewhere else, maybe a promise or not inside an angular scope?. 实际上听起来像是摘要循环的问题,就好像您是在将模型分配到其他地方一样,可能是一个承诺,还是不在一个角度范围内? If this is true, angular is not refreshing the view because it doesn't know about the change, but it refreshes in the digest cycle that occurs when you submit the form. 如果为真,则angular不会刷新视图,因为它不知道更改,但是会在提交表单时发生的摘要周期中刷新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM