Angularjs 以编程方式更改ng模型时未获取ng模型的更新值

Angularjs 以编程方式更改ng模型时未获取ng模型的更新值,angularjs,angular-ngmodel,Angularjs,Angular Ngmodel,当我选中主复选框时,应选中所有子复选框,并且在选中该复选框时也应显示其内容,否则不应显示其内容,但问题是,如果我手动选中子复选框,则选中主复选框时会选中子复选框,但不会显示其内容如图所示 <!DOCTYPE html> <html> <head> <title>Angular Demo</title> <script src="https://ajax.goo

当我选中主复选框时,应选中所有子复选框,并且在选中该复选框时也应显示其内容,否则不应显示其内容,但问题是,如果我手动选中子复选框,则选中主复选框时会选中子复选框,但不会显示其内容如图所示

<!DOCTYPE html>
    <html>
        <head>
            <title>Angular Demo</title>
            <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
        </head>
        <body data-ng-app="">
            <table>
                <tr>
                    <td>
                        <input type="checkbox" data-ng-model="parent"/>
                    </td>
                    <td data-ng-show="parent" class="left">
                        It's master
                    </td>
                </tr>
                <tr>
                    <td>
                        <input type="checkbox" data-ng-model="child_1" data-ng-checked="parent"/>
                    </td>
                    <td data-ng-show="child_1">
                        It's child 1
                    </td>
                </tr>
                <tr>
                    <td>
                        <input type="checkbox" data-ng-model="child_2" data-ng-checked="parent"/>
                    </td>
                    <td data-ng-show="child_2">
                        It's child 2
                    </td>
                </tr>
            </table>
        </body>
    </html>

角度演示
是主人
是孩子一号
是孩子2

您没有与子模型交互-您直接从父模型设置选中的属性

相反,请为您的家长检查handle ng:

<input type="checkbox" data-ng-model="parent" ng-checked="parentChanged()"/>
还要从视图的子节点中删除数据ng checked=“parent”

<input type="checkbox" data-ng-model="child_1">
...
<input type="checkbox" data-ng-model="child_2">

...
必须如下设置属性“child\u 1”:

<input type="checkbox" data-ng-model="child_1" data-ng-checked="child_1 = parent"/>


您可以看到如何做:

我已经解决了这个问题

<input type="checkbox" data-ng-model="parent" data-ng-change="child_1= parent;
                            child_2=parent;" />


请在此查看代码。

我想在选中父复选框以及选中子复选框时显示内容手动执行类似操作?此示例仅用于说明,您应该按照我的回答将此逻辑放入控制器。我无法按照您的建议操作,因为使用循环语句从服务器端输入是动态的。然后,我将通过ngRepeat将循环移动到客户端,这将允许您访问要在其上执行的数据对象。我已检查了您的代码,它在1.4.0版上工作。使用1.3.13版时出现语法错误。我无法在1.3.13版中分配多个变量,如下所示。子_1=子_2=父;我已经完成了使用下面的代码,它的工作。child_1=父母;child_2=父母;您的代码仅在选中“主”复选框时有效,但在选中“子”复选框时无效。我希望它应使用“主”复选框和“子”复选框以两种方式工作。仍然存在一个问题,我遵循了以下步骤。(1)我选中了“主”复选框现在所有复选框都已选中,并且它们的内容也显示出来。(2) 我未选中“第一个子项”复选框,但其内容未隐藏。如果您关心代码的易维护性和可读性,则从视图中设置这样的模型值不是一个好做法
<input type="checkbox" data-ng-model="parent" data-ng-change="child_1= parent;
                            child_2=parent;" />