[以编程方式更改ng-model时未获得ng-model的更新值

问题描述 投票:-1回答:3

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

<!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>
angularjs angular-ngmodel
3个回答
2
投票
我已经解决了问题。

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

请点击click查看代码。

1
投票
您未与子模型交互-您是直接从父模型设置选中的属性。

相反,为您的父母处理ng-checked:

<input type="checkbox" data-ng-model="parent" ng-checked="parentChanged()"/>

并且在您的控制器中,将每个孩子的模型设置为父值:

$scope.parentChanged = function(){ child_1 = parent; child_2 = parent; }

还从视图的子节点中删除data-ng-checked =“ parent”。

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


1
投票
您必须这样设置属性“ child_1”:

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

您可以看到如何做:Here
© www.soinside.com 2019 - 2024. All rights reserved.