【问题标题】:How to select radio button with ngFor inside each row having two radio buttons如何在具有两个单选按钮的每行内选择带有 ngFor 的单选按钮
【发布时间】:2019-04-04 05:38:22
【问题描述】:

本部分包含 HTML 部分,以显示 我如何迭代 对象数组


<div class="row" *ngFor="let item of modules; let i = index;">
                                        <div class="col-md-1 align-center">{{i+1}}</div>
                                        <div class="col-md-5">
                                            <input type="text" class="form-control" [(ngModel)]="modules[i].module_name" value="{{modules[i].module_name}}"
                                             disabled>
                                        </div>
                                        <div class="col-md-2">
                                            <input type="radio" class="form-control" [(ngModel)]="modules[i].action.read" name="access" [value]="modules[i].action.read">
                                        </div>
                                        <div class="col-md-2">
                                            <input type="radio" class="form-control" [(ngModel)]="modules[i].action.write" name="access" [value]="modules[i].action.write">
                                        </div>
                                        <div class="col-md-2">
                                            <input type="checkbox" class="form-control" [(ngModel)]="modules[i].flag">
                                        </div>
                                    </div>

这是我正在使用一些对象数组的参考对象

    modules = [{
    company_id: '0',
    role_id: 'xyz',
    module_name: 'xyz',
    action: {
        write: false,
        read: false
    },
    flag:false
  },{
    company_id: '0',
    role_id: 'xyz',
    module_name: 'xyt',
    action: {
        write: false,
        read: false
    },
    flag:false
  }];

【问题讨论】:

    标签: html json angular typescript ngfor


    【解决方案1】:

    为复选框使用唯一名称:

    参考:https://stackblitz.com/edit/angular-cymeqg?file=src%2Fapp%2Fapp.component.html

        <div class="row" *ngFor="let item of modules; let i = index;">
        <div class="col-md-1 align-center">{{i+1}}</div>
        <div class="col-md-5">
            <input type="text" class="form-control" [(ngModel)]="modules[i].module_name" value="{{modules[i].module_name}}" disabled>
    </div>
    
    <div class="col-md-2">
    <input type="radio" class="form-control" [checked]="modules[i].action.read" (change)="modules[i].action.read"  name="access_{{modules[i].company_id}}">
     </div>
    
    <div class="col-md-2">
    <input type="radio" [checked]="modules[i].action.write" (change)="modules[i].action.write" class="form-control" name="access_{{modules[i].company_id}}">
     </div>
    
     <div class="col-md-2">
    <input type="checkbox" class="form-control" [(ngModel)]="modules[i].flag">
    </div>
    
    </div>
    
    
    
    modules = [{
        company_id: '0',
        role_id: 'xyz',
        module_name: 'xyz',
        action: {
            write: false,
            read: false
        },
        flag:false
      },{
        company_id: '1',
        role_id: 'xyz',
        module_name: 'xyt',
        action: {
            write: false,
            read: false
        },
        flag:false
      }];
    

    【讨论】:

    • 谢谢!这在前端工作,但我也需要双向数据绑定,例如 modules[i].action.read 或 write
    猜你喜欢
    • 2018-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-14
    • 1970-01-01
    • 2022-11-24
    • 1970-01-01
    相关资源
    最近更新 更多