Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Angular 2中使用FormControl向Combobox添加值_Angular_Angular2 Forms - Fatal编程技术网

在Angular 2中使用FormControl向Combobox添加值

在Angular 2中使用FormControl向Combobox添加值,angular,angular2-forms,Angular,Angular2 Forms,更新 <select (blur)="stateValidate('State *Required', 'State Name')" class="form-control" id="state_address" formControlName="state_address"> <option *ngFor="let state of states">{{state}}</option> 但是当我尝试将状态添加到

更新

 <select (blur)="stateValidate('State *Required', 'State Name')" class="form-control" id="state_address" formControlName="state_address">
                      <option *ngFor="let state of states">{{state}}</option>
但是当我尝试将状态添加到组合框时,它不提供值

// this does not work.
    state_address: new FormControl(this.contractData.state_address, [
    Validators.required
  ]),
为什么会发生这种行为?根据存储在数据库中的用户数据向组合框添加值的正确方法是什么

HTML

 <form [formGroup]="contractForm" novalidate (ngSubmit)="saveContract(contractForm.value, contractForm.valid)" (window:resize)="beResponsive()">
      <div class="row">
        <div class="col-sm-12 col-md-12 col-lg-12">
          <div class="card card-inverse card-success">
            <div class="card-header">
              <strong>Personal Information</strong>
            </div>
            <div class="card-block">
              <!-- Start Phone Section -->
              <div class="row">
                <div class="form-group col-sm-12 col-md-4">
                  <label for="first_name">First Name</label>
                  <div class="input-group">
                                        <span class="input-group-addon"><i class="fa fa-user"></i>
                  </span>

                    <input (blur)="cityValidate('City *Required', 'City Name')" type="text" class="form-control" placeholder="Enter city name" id="city_address" formControlName="city_address">
                  </div>
                </div>
                <div class="form-group col-sm-12 col-md-4">
                  <label for="state_address">State</label>
                  <div class="input-group">
                                        <span class="input-group-addon"><i class="fa fa-street-view"></i>
                  </span>

您正在将选项的文本设置为
状态
。您应该像这样将
值设置为
状态

<option *ngFor="let state of states" [value]="state">{{ state }}</option>
{{state}

您确定
此.contractData.state\u地址不为null或空吗?是。我检查了它,当我执行{{this.contractData.state_address}时,它确实输出了texas,但是在组件中,我从中删除了this。但是我把这个放在其他的上面了。我甚至试过这个。。这很奇怪。我想知道这是否只是我的代码,或者这是一个正常的事情,因为它不起作用。你能添加用于此表单的html吗?我更新了我的代码。我试着去掉一些多余的东西,让你的眼睛更舒服。谢谢你的帮助!记住,其他一切都是有效的。只有国家不起作用。这恰好是组合框。我看不到html中的
formControlName=“state\u address”
和任何下拉列表。
  this.contractForm = new FormGroup({
      signatureField1 : new FormControl('',Validators.required ),
      email : new FormControl(this.contractData.email,Validators.required ),
      first_name: new FormControl(this.contractData.first_name,[
        Validators.required,
        Validators.pattern("^[a-zA-Zñáéíóúü']{1,30}$")
      ]),
      middle_name: new FormControl(this.contractData.middle_name,[
        Validators.required,
        Validators.pattern("^[a-zA-Zñáéíóúü']{1,30}$")
      ]),
      last_name: new FormControl(this.contractData.last_name,[
        Validators.required,
        Validators.pattern("^[a-zA-Zñáéíóúü']{1,30}$")
      ]),
      employer: new FormControl(null, [
        Validators.required,
        Validators.pattern("^[a-zA-Zñáéíóúü' ]{1,30}$")
      ]),
      dob : new FormControl (this.contractData.dob, [
        Validators.required,
        Validators.pattern("[1][9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]|[2][0][0-9][0-9]-[0-9][0-9]-[0-9][0-9]")
      ]),
      client_ss: new FormControl(this.contractData.client_ss, [
        Validators.required,
        Validators.pattern("[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]|[0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9]|[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]")
      ]),
      phone: new FormControl(this.contractData.phone, [
        Validators.required,
        Validators.pattern("[0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]|[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]"),
      ]),
      work_phone: new FormControl(null, [
        Validators.required,
        Validators.pattern("[0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]|[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]"),
      ]),
      fax_phone: new FormControl(null, [
        Validators.required,
        Validators.pattern("[0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]|[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]"),
      ]),
      street_address: new FormControl (this.contractData.street_address, [
        Validators.required,
        Validators.pattern("^[0-9]+ .+$")
      ]),
      city_address: new FormControl(this.contractData.city_address, [
        Validators.required,
        Validators.pattern("^[a-zA-Zñáéíóúü' ]{1,30}$")
      ]),
      state_address: new FormControl(this.contractData.state_address, [
        Validators.required
      ]),
      zip_address: new FormControl(this.contractData.zip_address, [
        Validators.required,
        Validators.pattern("[0-9][0-9][0-9][0-9][0-9]")
      ]),
      client_dl: new FormControl(null, [
        Validators.required
      ]),
      dl_state: new FormControl(null, [
        Validators.required
      ]),
    });
<option *ngFor="let state of states" [value]="state">{{ state }}</option>