如何让多个单选按钮分组在angularjs中

时间:2016-04-23 15:15:34

标签: javascript angularjs

我是angularjs的新手。我想为一个用户添加我的代码和架构更新角色集,在此代码下面我无法获得我喜欢的对象,请查看以下代码。

HTML:

<section class="surround" style="overflow-y:scroll;min-height: 600px;">
<form name="userrole" ng-submit="addRoles();" novalidate>
<div class="row">
    <div class="col-xs-4">
        <div class="row form-group">
            <div class="col-xs-offset-5">
                <h4>Create User</h4>
            </div>
        </div>
        <div class="row form-group" ng-class="{ 'has-error': submitted && branchForm.name.$error.required }">
            <div class="col-xs-5 form-label">
                <label>Roles name</label>
            </div>
            <div class="col-xs-6 control">
                <input type="text" class="form-control" ng-model="roles.rolename" name="rolename" required="" />
            </div>
        </div>

        <div class="row form-group">
            <div class="col-xs-5 form-label">
                <label>Status</label>
            </div>
            <div class="col-xs-6 control">
                <select class="form-control" ng-model="roles.status" name="status">
                    <option value="1">Active</option>
                    <option value="0">Deleted</option>
                </select>
            </div>
        </div>
    </div>  
</div>    
<div class="row">
    <div class="col-xs-10">
        <div class="branch-table">
            <table class="table table-bordered">
                <colgroup>
                    <col width="20%">
                    <col width="20%">
                    <col width="20%">
                   <col width="20%">
                    <col width="20%">
                </colgroup>
                <thead>
                    <tr>
                        <th>Transaction</th>
                        <th>No Access</th>
                        <th>Read Only</th>
                        <th>Read/Write</th>
                        <th>Full</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td><label for="Accounting"><input     type="checkbox" id="Accounting" ng-true-value="Accounting" ng-model="roles.transaction.name">Accounting</label></td>
                        <td><input type="radio" name="group1" ng-model="roles.transaction.access.noaccess" data-ng-value="true" /></td>
                        <td><input type="radio" name="group1" ng-model="roles.transaction.access.read_permit" data-ng-value="true" /></td>
                        <td><input type="radio" name="group1" ng-model="roles.transaction.access.write_permit" data-ng-value="true" /></td>
                        <td><input type="radio" name="group1" ng-model="roles.transaction.access.full" data-ng-value="true" /></td>                           
                    </tr>  
                    <tr>
                        <td><label for="Accounting"><input type="checkbox" id="Accounting" ng-true-value="Accounting" ng-model="roles.transaction.name">Property</label></td>
                        <td><input type="radio" name="group2" ng-model="roles.transaction.access.noaccess" data-ng-value="true" /></td>
                        <td><input type="radio" name="group2" ng-model="roles.transaction.access.read_permit" data-ng-value="true" /></td>
                        <td><input type="radio" name="group2" ng-model="roles.transaction.access.write_permit" data-ng-value="true" /></td>
                        <td><input type="radio" name="group2" ng-model="roles.transaction.access.full" data-ng-value="true" /></td>                           
                    </tr>
                    <tr>
                        <td><label for="Accounting"><input type="checkbox" id="Accounting" ng-true-value="Accounting" ng-model="roles.transaction.name">Contact</label></td>
                        <td><input type="radio" name="group3" ng-model="roles.transaction.access.noaccess" data-ng-value="true" /></td>
                        <td><input type="radio" name="group3" ng-model="roles.transaction.access.read_permit" data-ng-value="true" /></td>
                        <td><input type="radio" name="group3" ng-model="roles.transaction.access.write_permit" data-ng-value="true" /></td>
                        <td><input type="radio" name="group3" ng-model="roles.transaction.access.full" data-ng-value="true" /></td>                           
                    </tr>

               </tbody>
            </table>
        </div>
   </div>
</div>

<div class="row">
    <div class="col-xs-4 form-group">
        <div class="pull-left">
            <button type="submit" class="btn btn-sm btn-primary">Save</button>
            <button ng-click="cancel()" class="btn btn-sm btn-default">Cancel</button>         
        </div>
    </div> 
</div>
</form>
</section>

点击保存后,我得到这样的对象,

{
"rolesname": "superadmin",
"status": 1,

"transaction": [
    {
        "name": "property",
        "access": {
            "noaccess": "true",
        }
    }
]

}

我一次只能获得一个物体。

我想得到像这样的对象,

对象数据:

{
"_id": "ROLE01",
"description": "superadmin",
"status": "active",

"transaction": [
    {
        "name": "property",
        "access": {
            "read_permit": "true"
        }
    },
    {
        "name": "property",
        "access": {
            "read_permit": "true"
        }
    }
    {
        "name": "property",
        "access": {
            "read_permit": "true"
        }
    }
]

}

我没有将该交易作为三个对象。如何以棱角分明的方式实现这一点,我不知道我在做什么。谢谢你的进步。

1 个答案:

答案 0 :(得分:0)

您在同一对象上绑定相同的属性。在您的示例中,transaction是一个对象,而不是一个数组。 Group1值由group2值覆盖,然后由group3值覆盖。

这解决了您的问题:

<tr>
    <td><label for="Accounting"><input     type="checkbox" id="Accounting" ng-true-value="Accounting" ng-model="roles.transaction[0].name">Accounting</label></td>
    <td><input type="radio" name="group1" ng-model="roles.transaction[0].access.noaccess" data-ng-value="true" /></td>
    <td><input type="radio" name="group1" ng-model="roles.transaction[0].access.read_permit" data-ng-value="true" /></td>
    <td><input type="radio" name="group1" ng-model="roles.transaction[0].access.write_permit" data-ng-value="true" /></td>
    <td><input type="radio" name="group1" ng-model="roles.transaction[0].access.full" data-ng-value="true" /></td>                           
</tr>  
<tr>
    <td><label for="Accounting"><input type="checkbox" id="Accounting" ng-true-value="Accounting" ng-model="roles.transaction[1].name">Property</label></td>
    <td><input type="radio" name="group2" ng-model="roles.transaction[1].access.noaccess" data-ng-value="true" /></td>
    <td><input type="radio" name="group2" ng-model="roles.transaction[1].access.read_permit" data-ng-value="true" /></td>
    <td><input type="radio" name="group2" ng-model="roles.transaction[1].access.write_permit" data-ng-value="true" /></td>
    <td><input type="radio" name="group2" ng-model="roles.transaction[1].access.full" data-ng-value="true" /></td>                           
</tr>
<tr>
    <td><label for="Accounting"><input type="checkbox" id="Accounting" ng-true-value="Accounting" ng-model="roles.transaction[2].name">Contact</label></td>
    <td><input type="radio" name="group3" ng-model="roles.transaction[2].access.noaccess" data-ng-value="true" /></td>
    <td><input type="radio" name="group3" ng-model="roles.transaction[2].access.read_permit" data-ng-value="true" /></td>
    <td><input type="radio" name="group3" ng-model="roles.transaction[2].access.write_permit" data-ng-value="true" /></td>
    <td><input type="radio" name="group3" ng-model="roles.transaction[2].access.full" data-ng-value="true" /></td>                           
</tr>