【问题标题】:AngularJS Change Checked Value of Radio Button Using $scope variableAngularJS 使用 $scope 变量更改单选按钮的选中值
【发布时间】:2015-11-26 10:36:28
【问题描述】:

我正在编辑用户表单。我使用 $scope 对象将数据从控制器发送到编辑视图以编辑表单。数据如下:

        $scope.changeUser = [

          {

                id: 1,
                username: 'Ramesh',
                password: 'Ramesh1@23',
                role: 'admin',
                active: 'no'
            }
    ];
       <div class="form-group">
                        <label class="control-label col-md-3">Action</label>
                        <div class="col-md-4">
                            <div class="radio-list">
                                <label class="radio-inline">
                                    <input type="radio" name="optionsRadios2"  data-ng-model="changeUser.active"  value="yes"/>
                                    Yes
                                </label>
                                <label class="radio-inline">
                                    <input type="radio" name="optionsRadios2" data-ng-model="changerUser.active" value="no"/>
                                    No
                                </label>
                            </div>
                        </div>

                    </div>

当编辑表单得到{{changeUser.action}} 时,我必须相应地检查单选按钮。就像action=='no' 名称为no 的单选按钮应该被自动检查,就像我们在html 中使用checked value=no 所做的那样。我必须编写 ng-if 条件来查看操作值。

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    您的ng-model 中缺少changeUser 数组的索引。

        <div class="form-group">
            <label class="control-label col-md-3">Action</label>
            <div class="col-md-4">
                <div class="radio-list">
                    <label class="radio-inline">
                        <input type="radio" name="optionsRadios2" data-ng-model="changeUser[0].active" value="yes" />
                        Yes
                    </label>
                    <label class="radio-inline">
                        <input type="radio" name="optionsRadios2" data-ng-model="changeUser[0].active" value="no" />
                        No
                    </label>
                </div>
            </div>    
        </div>
    

    See the Plnkr

    【讨论】:

    • 这不是我要找的答案。我问的是根据从@scope `action: yes or no' 得到的值来做checked value=nochecked value=yes
    • @user3789184 上述解决方案将起作用。由于角度会将“ng-model”的值与分配给无线电的值进行比较。如果匹配,相应的收音机将被选中。
    • 但是,当我编辑表单时,$scope 上只有一个数据。所以,在这种情况下,我不能使用ng-repeat
    • 如果您不想使用 ng-repeat,请删除第一个 div 并将 data-ng-model 作为 changeUser[0].active 传递给两个无线电输入.
    • @user3789184 更新了我的答案。看看吧。
    猜你喜欢
    • 2021-04-03
    • 1970-01-01
    • 2017-07-22
    • 2016-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多