【问题标题】:Not getting the updated value of ng-model when ng-model is changed programmatically以编程方式更改 ng-model 时未获取 ng-model 的更新值
【发布时间】:2015-02-25 12:51:01
【问题描述】:

当我选中主复选框时,应选中所有子复选框,并且在选中复选框时也应显示其内容,否则不应显示其内容,但问题是子复选框已选中但是如果我手动检查子复选框,则当我检查主复选框时,它们的内容不会显示出来。

<!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


    【解决方案1】:

    我已经解决了这个问题。

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

    请click在这里查看代码。

    【讨论】:

    • 如果您关心代码的易维护性和可读性,那么从视图中设置这样的模型值并不是一个好习惯
    【解决方案2】:

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

    相反,为你的父母处理 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">
    

    【讨论】:

    • 我想在选中父复选框以及手动选中子复选框时显示内容
    • 这样的? plnkr.co/edit/iFAsXMS4dYQMYFhXcOcL?p=preview此示例仅用于说明,您应该按照我的回答中所述将此逻辑放入控制器中
    • 我不能按照你的建议做,因为输入是使用循环语句从服务器端动态输入的。
    • 然后我会通过 ngRepeat 将循环移动到客户端,这样您就可以访问在 parentChanged() docs.angularjs.org/api/ng/directive/ngRepeat 中执行 .forEach 的数据对象
    • 我检查了您的代码,它适用于 1.4.0 版,使用 1.3.13 版时出现语法错误。我无法在 1.3.13 版中分配多个变量,如下所示。 child_1=child_2=父母;我已经使用下面的代码完成了并且它有效。 child_1=父母;child_2=父母;
    【解决方案3】:

    您必须像这样设置属性“child_1”:

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

    你可以看看怎么做:Here

    【讨论】:

    • 您的代码仅在选中主复选框时有效,但当我选中子复选框时它不起作用。我希望它应该使用主复选框和子复选框以两种方式工作复选框。
    • 仍然存在问题,我按照以下步骤操作。(1)我检查了主复选框,现在所有的复选框都被选中并且它们的内容也被显示出来。 (2) 我取消选中第一个子复选框,但它的内容没有隐藏。
    猜你喜欢
    • 1970-01-01
    • 2023-03-26
    • 2017-09-22
    • 2015-08-22
    • 2015-08-13
    • 2016-12-09
    • 2014-10-13
    • 1970-01-01
    相关资源
    最近更新 更多