Angular -- form表单中使用*ngFor循环

1,实体示例如下,Blog对象中包含子属性Post列表。

Entity:

Blog : List<Post> Posts

Post : string Name

            List<string> typeList

2,Component中使用Form表单对Blog对象进行显示和修改。

<form nz-form #blogForm="ngForm" [nzLayout]="'vertical'">
  <div>
     <nz-table #blogTable [nzData]="Blog.Posts" nzShowPagination="false">
        <tr *ngFor="let item of blogTable.data ; let i = index">
           <td>
              <label nz-checkbox [(ngModel)]="item.isChecked" name="{{i}}+isChecked">{{item.name}}</label>
           </td>
           <td>
             <nz-form-item>
                <nz-form-label [nzSpan]="24">Type</nz-form-label>
                   <nz-form-control [nzSpan]="24">
                       <nz-select [(ngModel)]="item.typeList" nzAllowClear nzPlaceHolder="Please Select" [nzSuffixIcon]="templateS" name="{{i}}+typeList"
nzMode="multiple">
                        <nz-option *ngFor="let postType of postTypeList" [nzValue]="postType.name" [nzLabel]="postType.name"></nz-option>
                       </nz-select>
                 </nz-form-control>
             </nz-form-item>
            </td>
          </tr>
      </nz-table>
  </div>
</form>

1) post 对象的type支持多选,下拉框选择值为postTypeList。

当前示例中postTypeList列表里的对象为enum类型,下拉框显示为enum的name值,选择后保存到post中的值也为string类型。

也可以将postTypeList里的类型换成string,此时<nz-option> 中 [nzValue] ="postType",[nzLabel] ="postType"。

2)form表单项的每个[(ngModel)]绑定一个对象属性,且有唯一的name值,但是当使用for循环时,每条记录的相同属性共用一个name,此时从数据库读取到的值不能正确显示到每个input、CheckBox、select,需要分别命名name。本示例采用的方法为: name="{{i}}+isChecked",为每个name添加一个变量。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值