Angular2:以动态形式获取输入值

时间:2017-09-04 12:13:27

标签: angular2-forms dynamic-forms

我需要以动态形式获得某个输入的值。

我有像这样的JSON参数

{  
     "etiquette":"Téléphone mobile",
     "ordre":1,
     "obligatoire":true,
     "pattern":"^(?:(?:(?:\\\\+|00)33[ ]?(?:\\\\(0\\\\)[ ]?)?)|0){1}[1-9]{1}([ .-]?)(?:\\\\d{2}\\\\1?){3}\\\\d{2}$",
     "section":"TelMail",
     "type":"text",
     "nom":"telephoneMobile",
     "texteIndice":"Téléphone mobile"
  }
,
  {  
     "etiquette":"Mail",
     "ordre":2,
     "obligatoire":true,
     "pattern":"(?:[a-zA-Z0-9!#$%&''*+=?^_`{|}~-]+(?:\\\\.[a-zA-Z0-9!#$%&''*+=?^_`{|}~-]+)*|\u201d(?:[\\\\x01-\\\\x08\\\\x0b\\\\x0c\\\\x0e-\\\\x1f\\\\x21\\\\x23-\\\\x5b\\\\x5d-\\\\x7f]|\\\\\\\\[\\\\x01-\\\\x09\\\\x0b\\\\x0c\\\\x0e-\\\\x7f])*\u201d)@(?:(?:[a-zA-Z0-9àâäçéèëêîïôôûüù](?:[a-zA-Z0-9-àâäçéèëêîïôôûüù]*[a-zA-Z0-9àâäçéèëêîïôôûüù])?\\\\.)+[a-zA-Z0-9àâäçéèëêîïôôûüù](?:[a-zA-Z0-9-àâäçéèëêîïôôûüù]*[a-zA-Z0-9àâäçéèëêîïôôûüù])?|\\\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-zA-Z0-9-]*[a-zA-Z0-9]:(?:[\\\\x01-\\\\x08\\\\x0b\\\\x0c\\\\x0e-\\\\x1f\\\\x21-\\\\x5a\\\\x53-\\\\x7f]|\\\\\\\\[\\\\x01-\\\\x09\\\\x0b\\\\x0c\\\\x0e-\\\\x7f])+)\\\\])",
     "section":"TelMail",
     "type":"email",
     "nom":"mail",
     "texteIndice":"Mail"
  },
{  
     "etiquette":"Type alerte",
     "ordre":3,
     "obligatoire":true,
     "options": [{
                 "code" : "SMS",
                 "valeur" : "Alertes SMS"
                  },
                  {
                  "code" : "MAIL",
                  "valeur" : "Alertes Mail"
      }],
     "section":"Alerte",
     "type":"radio",
     "nom":"typeAlerte",
     "texteIndice":"Type alerte"
  }

我有如下面的界面截图。 this

我定制了无线电组件,用于包含邮件和电话的无线电输入和输入。 我需要的是:获取输入电话/邮件的价值并输入与收音机相关的输入。 这是我的无线电组件:

    import {Component,Inject,Input} from '@angular/core';
import {NgForm, FormGroup} from '@angular/forms';
import {ExtraFormField} from '../model/form';
import {ExtraField} from './extra-field';
import { CatalogueService } from "../../catalogue/catalogue.service";

@Component({
    selector: 'radio-extra-field',
    template:`
            <div class="form-group" >
                <label [attr.for]="field.nom">{{field.etiquette}}</label>
                <div *ngFor="let option of field.options" >
                    <input type="radio" name ="{{field.nom}}" value="{{option.code}}" id="{{option.code}}" [(ngModel)]="typeSelectionne">{{option.valeur}}
                    <input id="{{option.code}}" [attr.title]="field.etiquette" [attr.minlength]="field.longueurMin" [attr.min]="field.min" [attr.max]="field.max"
                        [attr.maxlength]="field.longueurMax"  [attr.value]="field.valeur"
                        [attr.type]="text" [formControl]="fieldControl" (change)="maj(id.value)"
                        [attr.id]="option.code" type="text" [attr.disabled]="typeSelectionne != option.code? disabled : null ">

                    <error-messages [control]="field.nom"></error-messages>
                </div>
                <error-messages [control]="field.nom"></error-messages>
            </div>

    `
})

export class RadioExtraField extends ExtraField {
        typeSelectionne: string;
        @Input() field:ExtraFormField;
        @Input() entity:{fields:Object};
        @Input() formGroup:FormGroup;

    constructor(public catalogueService: CatalogueService, @Inject(NgForm) formDir: NgForm) {
        super(null, catalogueService, formDir);
    }
    get disabled():string {
        if(this.field) {
            return 'disabled';
        }
        return null;
    }
}

有办法做到这一点吗?

谢谢

1 个答案:

答案 0 :(得分:0)

您可以使用

(ngModelChange)

它的作用是调用函数,在那里你可以为输入字段的ngModel赋值。