Forms Angular 2模板驱动表单-数组输入元素

Forms Angular 2模板驱动表单-数组输入元素,forms,angular,angularjs-ng-repeat,Forms,Angular,Angularjs Ng Repeat,我已经挣扎了一天半,仍然找不到任何解决问题的方法。我正在处理一个简单的表单,它有select和复选框元素。当我尝试提交表单时,我没有得到select和复选框的值,而是在控制台中得到true。 我正在开发模板驱动的表单 <form (ngSubmit)="addSubcontractor(subcontractorForm)" #subcontractorForm="ngForm"> <h5>Type :</h5> <div

我已经挣扎了一天半,仍然找不到任何解决问题的方法。我正在处理一个简单的表单,它有
select
复选框
元素。当我尝试提交表单时,我没有得到
select
复选框
的值,而是在控制台中得到true。 我正在开发模板驱动的表单

<form (ngSubmit)="addSubcontractor(subcontractorForm)" #subcontractorForm="ngForm">
      <h5>Type :</h5>
         <div class="form-group">
             <div *ngFor="let contractor_type of contractor_types;let i = index;" class="pull-left margin-right">
             <label htmlFor="{{ contractor_type | lowercase }}">
                            {{ contractor_type }} :
            </label>
            <input type="checkbox" name="_contractor_type[{{i}}]" [value]="contractor_type" ngModel>
        </div>
    <div class="form-group">
        <select class="form-control costcodelist" name="_cc_id[]"  multiple="true" ngModel>
        //When I put ngModel on select I just keep getting error
        //TypeError: values.map is not a function
           <option *ngFor="let costcode of costcodes" [selected]="costcode.id == -1" [value]="costcode.id">{{ costcode.costcode_description }}</option>
         </select>
    </div>
   </form>
    <form #f="ngForm" (submit)="addOperator(f.valid)" novalidate>
        <label>Name</label>

        <input [(ngModel)]="operator.name" name="name">
        <label >Service</label>
        <select #service="ngModel" name="service" [(ngModel)]="operator.service">
            <option *ngFor='let service of services' [ngValue]='service'>{{service.name}}</option>
        </select>

        <label >Enabled</label>
        <label>
            <input type="checkbox" name="enabled" [(ngModel)]="operator.enabled">
        </label>

        <button type="submit">Create Operator</button>
    </form>
当我从
select
元素中删除
ngModel
时,代码运行良好,当我提交表单时,我得到以下输出

_contractor_type[0]:true
_contractor_type[1]:true
_contractor_type[2]:true 
_contractor_type[3]:""
我没有从select元素中获取复选框值以及所选选项

您的意见和答复将不胜感激


谢谢

这里是一个简单的选择,我在模板驱动表单中使用它

<form (ngSubmit)="addSubcontractor(subcontractorForm)" #subcontractorForm="ngForm">
      <h5>Type :</h5>
         <div class="form-group">
             <div *ngFor="let contractor_type of contractor_types;let i = index;" class="pull-left margin-right">
             <label htmlFor="{{ contractor_type | lowercase }}">
                            {{ contractor_type }} :
            </label>
            <input type="checkbox" name="_contractor_type[{{i}}]" [value]="contractor_type" ngModel>
        </div>
    <div class="form-group">
        <select class="form-control costcodelist" name="_cc_id[]"  multiple="true" ngModel>
        //When I put ngModel on select I just keep getting error
        //TypeError: values.map is not a function
           <option *ngFor="let costcode of costcodes" [selected]="costcode.id == -1" [value]="costcode.id">{{ costcode.costcode_description }}</option>
         </select>
    </div>
   </form>
    <form #f="ngForm" (submit)="addOperator(f.valid)" novalidate>
        <label>Name</label>

        <input [(ngModel)]="operator.name" name="name">
        <label >Service</label>
        <select #service="ngModel" name="service" [(ngModel)]="operator.service">
            <option *ngFor='let service of services' [ngValue]='service'>{{service.name}}</option>
        </select>

        <label >Enabled</label>
        <label>
            <input type="checkbox" name="enabled" [(ngModel)]="operator.enabled">
        </label>

        <button type="submit">Create Operator</button>
    </form>

这里是一个简单的选择,我使用我的模板驱动表单

<form (ngSubmit)="addSubcontractor(subcontractorForm)" #subcontractorForm="ngForm">
      <h5>Type :</h5>
         <div class="form-group">
             <div *ngFor="let contractor_type of contractor_types;let i = index;" class="pull-left margin-right">
             <label htmlFor="{{ contractor_type | lowercase }}">
                            {{ contractor_type }} :
            </label>
            <input type="checkbox" name="_contractor_type[{{i}}]" [value]="contractor_type" ngModel>
        </div>
    <div class="form-group">
        <select class="form-control costcodelist" name="_cc_id[]"  multiple="true" ngModel>
        //When I put ngModel on select I just keep getting error
        //TypeError: values.map is not a function
           <option *ngFor="let costcode of costcodes" [selected]="costcode.id == -1" [value]="costcode.id">{{ costcode.costcode_description }}</option>
         </select>
    </div>
   </form>
    <form #f="ngForm" (submit)="addOperator(f.valid)" novalidate>
        <label>Name</label>

        <input [(ngModel)]="operator.name" name="name">
        <label >Service</label>
        <select #service="ngModel" name="service" [(ngModel)]="operator.service">
            <option *ngFor='let service of services' [ngValue]='service'>{{service.name}}</option>
        </select>

        <label >Enabled</label>
        <label>
            <input type="checkbox" name="enabled" [(ngModel)]="operator.enabled">
        </label>

        <button type="submit">Create Operator</button>
    </form>

关于
multi-select
元素,还有它是双向数据绑定,在我的情况下,这是相当不好的。对于multi-select im,使用MIT许可证的
angular-2-dropdown-multiselect
dependencie:所以这实际上是一个问题,有了这个dependencie,你可以根据需要设置样式,所以这不是一个大问题。如果你想,我可以用
angular-2-dropdown-multiselect
的实现编辑答案,那么
multi-select
元素呢,而且它是双向数据绑定,在我的情况下,这对multi-select im很不好,使用
angular-2-dropdown-multiselect
dependencie和MIT许可证:所以这实际上是个问题是的,有了这个dependencie,你可以随心所欲地设计它,所以这不是什么大问题。如果需要,我可以使用
angular-2-dropdown-multiselect