简体   繁体   English

通过javascript选择输入复选框时未触发ng-show

[英]ng-show not triggered when input checkbox selected via javascript

I have an html page where clicking on a checkbox causes a new section of the page to become visible. 我有一个html页面,单击复选框会导致页面的新部分变为可见。 The HTML looks like this: HTML看起来像这样:

            <input id="formcheck" ng-model="collapseform" type="checkbox">Fill in the Test Form         
            <div id="mainformdiv" class="wizard-div" ng-show="collapseform">                
                    <div>                           
                        <div class="main-block">    
                                ...and so on
                        </div>
                    </div>
            </div>

This part works fine - by default that part of the page is hidden, and when I select the checkbox, the rest of the page appears. 这部分工作正常-默认情况下,该页面的一部分是隐藏的,当我选中该复选框时,将显示页面的其余部分。 When I deselect it, the rest of the page is hidden again. 当我取消选择它时,页面的其余部分再次被隐藏。

Further down on that same page there is a table, and when you select a node in that table it calls a controller function that causes some internal values to be set - this part works as well, so the controller seems to be set up correctly and all the functions can be called. 在同一页面的下一层,有一个表,当您在该表中选择一个节点时,它会调用一个控制器函数,该控制器函数会导致一些内部值的设置-该部分也起作用,因此控制器似乎已正确设置,并且所有功能都可以调用。

The problem I'm seeing is this: I want to add a bit to the controller function that gets called when the table node is selected, and what I want it to do is, in addition to setting the internal values (which it does correctly), also check the checkbox, and cause the hidden part of the page to be displayed in response. 我看到的问题是:除了设置内部值(它可以正确执行)之外,我还想向选择表节点时被调用的控制器函数添加一些内容,而我希望它做的是),还请选中该复选框,并作为响应显示页面的隐藏部分。 I thought if I checked the checkbox through the javascript that would accomplish this, so I added this to the internal value setting function: 我以为如果通过javascript选中了复选框即可完成此操作,因此我将其添加到了内部值设置功能中:

        if (document.getElementById("formcheck").checked == false) {
            document.getElementById("formcheck").checked = true;
        }

That kind of works - the checkbox does get checked off when that piece of code is called, however, unlike when I click the checkbox with a mouse, it does not seem to trigger the ng-show, and so the hidden part of the page is not made visible. 这种工作-调用该代码段时,该复选框确实被选中,但是,与我用鼠标单击该复选框不同,它似乎不会触发ng-show,因此页面的隐藏部分不可见。 Is there something else I need to do in order to make this work? 为了完成这项工作,我还需要做其他事情吗?

Thanks! 谢谢!

You should be able to change the value of the model. 您应该能够更改模型的值。 Since NgModel provides two-way binding, it watches the $scope variable as well. 由于NgModel提供双向绑定,因此它也会监视$scope变量。

For example do 例如做

$scope.collapseform = false

instead of 代替

if (document.getElementById("formcheck").checked == false) {
    document.getElementById("formcheck").checked = true;
}

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

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