【问题标题】:Angular2 + binding selected option + ngFormAngular2 +绑定选择选项+ ngForm
【发布时间】:2017-06-22 08:17:53
【问题描述】:

我怎样才能让选定的项目提交给服务?像 form.value['name'] 这样的做法适用于字段名称,但不适用于字段国家/地区。

我尝试了一些我在 selectedCountry 中找到的解决方案,但我想我不明白该怎么做。

注意事项:

  • 仅(在我看来)相关代码如下所示
  • countries 是一个带有键“id”、“code”和“name”的 json,并且正在从相应的服务中正确检索

signup.component.html

<form (ngSubmit)="onSubmit(f)" #f="ngForm">
    <div class="input-field">
      <input type="text" id="name" name="name" class="validate" ngModel>
      <label for="name" data-error="nome inválido">Nome</label>
    </div>
    <div class="input-field">
       <select id="country" name="country" [(ngModel)]="selectedCountry">
         <option [ngValue]="" selected>Escolha um país</option>
         <option *ngFor="let country of countries" [ngValue]="country" > {{country.code}} - {{country.name}} </option>
       </select>
       <label for="country">País onde reside</label>
    </div>
    <button class="btn btn-primary" type="submit">Sign Up</button>
</form>

signup.component.ts

export class SignupComponent implements OnInit {
  countries: any[];
  selectedCountry: Object = {};

  onSubmit(form: NgForm) {
    this.authService
        .signUp(form.value['name'], form.value['country'])
        .subscribe(signUpResult => {
            //some code
        });
  }
}

【问题讨论】:

  • 你尝试过使用响应式表单吗?
  • 您能否再解释一下,以便我搜索更多“关键字”?谢谢。
  • 您能尝试在 plunker 中重现该问题吗?尝试了你的代码,它对我来说很好:)

标签: angular angular2-forms angular-ngmodel


【解决方案1】:

请尝试以下方式

@Component({
      selector: 'my-app',
      template:`
        <h1>Selecting Number</h1>
        <select type="number" [(ngModel)]="levelNum" (ngModelChange)="toNumber()">
          <option *ngFor="let level of levels" [ngValue]="level.num">{{level.name}}</option>
        </select>
        {{levelNum}}
      `,
    })
    export class AppComponent {
      levelNum:number;
      levels:Array<Object> = [
          {num: 0, name: "AA"},
          {num: 1, name: "BB"}
      ];

      toNumber(){
        this.levelNum = +this.levelNum;
        console.log(this.levelNum);
      }
    }

【讨论】:

    【解决方案2】:

    app.module.ts

    import { FormsModule } from '@angular/forms';
    
    @NgModule({
      bootstrap: [ AppComponent ],
      declarations: [
        AppComponent,
        // .. some other components
    
      ],
      imports: [ // import Angular's modules
        BrowserModule,
        FormsModule
        // .. some other modules
      ],
      providers: []
    })
    export class AppModule {}
    

    你的.component.ts

    @Component({
          selector: 'my-app',
          template:`
            <select id="country" name="country" [(ngModel)]="selectedCountry">
             <option value="">Escolha um país</option>
             <option *ngFor="let country of countries" [ngValue]="country" > {{country.code}} - {{country.name}} </option>
           </select>
          `,
        })
        export class AppComponent {
          public selectedCountry;
        }
    

    编辑

    【讨论】:

    • 谢谢,但是在这样做之后呢?我应该在组件上做什么才能获得选择的选项?
    • 所以在您使用 [(ngModel)] 的情况下, selectedCountry 未填充所选选项值?
    • 没错! selectedCountry 被称为未定义。
    • 我刚刚对其进行了测试,如果您在NgModule imports 数组中导入了FormsModule .... 和选项标签中的[ngValue] ;] 它工作得很好
    • FormsModule 来自 /forms 而 NgModule 来自 /core。您确定要说相同的导入数组吗?
    猜你喜欢
    • 2017-02-06
    • 2016-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-12
    • 1970-01-01
    • 2017-07-02
    • 1970-01-01
    相关资源
    最近更新 更多