如何在窗体内的自定义控件中使用ngModel? [英] How to use ngModel with a custom control inside a form?

查看:65
本文介绍了如何在窗体内的自定义控件中使用ngModel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个用于处理选择框的组件,现在在提交表单后将其放入表单标记中时,选择结果不会显示在控制台中.

I created a component for handling select box, now when I put it in form tag after submitted form the result of the selection doesn't show up in console.

我的代码有什么问题?我该如何解决?

What's the problem with my code? how can I fix this?

  • testOption:是我传递的对象数组,用@Input抛出选择框.
  • testOption: is array of object I passed throw the select box with @Input.

这是选择框组件:

import { Component, OnInit, Input } from '@angular/core';

@Component({
    selector: 'input-select',
    template:`
      <div class="field-select">
        <span><icon name="select-arrow" size="10"></icon></span>
        <select name="{{name}}" class="field">
          <option value="0" disabled selected>{{label}}</option>
          <option *ngFor="let option of options" [ngValue]="option.value">{{option.name}}</option>
        </select>
      </div>
    `
})
export class InputSelectComponent implements OnInit {
    @Input() label: string;
    @Input() name: string;
    @Input() options;


    // testOptions = [
    //   {value:'test',name:'test2'},
    //   {value:'test',name:'test2'}
    // ];

    constructor() { }

    ngOnInit() {
      console.log(this.options);
     }

}

在html中的用法:

<input-select label="test" name="select2" [options]="testOption"></input-select>

表单html:

<form role="form" class="form" #f="ngForm" (ngSubmit)="onSubmit(f)">
    <input class="field" name="name" ngModel type="text" placeholder="n1">
    <input-select label="b2" name="select2" [options]="testObject"></input-select>
    <input class="field" name="building-type" type="text" ngModel placeholder="b3">
</form>

控制台日志(没有选择框值)

Object {name: "test", building-type: "tset" }

推荐答案

我想我现在遇到了您的问题.

I guess I got your problem now.

您想在自定义组件上实现ControlValueAccessor,以便在具有ngModel的表单内使用它!!

You want to implement ControlValueAccessor on your custom component to use it inside of a form with ngModel!?

您的组件应如下所示:

@Component({
   selector: 'ng2-input-select',
   template: `
      <div class="field-select">
        <select name="{{ name }}" class="field" [(ngModel)]="value" (ngModelChange)="_onChange($event)">
          <option value="" disabled selected>{{ label }}</option>
          <option *ngFor="let option of options" [value]="option.value">{{ option.name }}</option>
        </select>
      </div>
   `,
   providers: [
      { /* idk why or what this will do exactly.. but it works! ;) */
         provide: NG_VALUE_ACCESSOR,
         useExisting: forwardRef(() => SelectBoxComponent),
         multi: true
      }
   ]
})
export class SelectBoxComponent implements OnInit, ControlValueAccessor {
  @Input() label: string;
  @Input() name: string;
  @Input() options;
  @Input() value: string = '';

  // ControlValueAccessor implementation
  // ====================================

  // call if value was changed inside our component
  private _onChange = (_: any) => { };
  // call if input was "touched" .. !
  private _onTouched = () => { };

  // incoming change..
  public writeValue(val: any) {
    this.value = val;
  }

  public registerOnChange(fn: (_: any) => void): void { this._onChange = fn; }
  public registerOnTouched(fn: () => void): void { this._onTouched = fn; }
}

实时演示: https://plnkr.co/edit/imCJmCoJaeGQiUMcyBwz?p=preview

更新

在表单组件中使用更改检测:

Using change detection in your form-component:

<ng2-input-select ngModel (ngModelChange)="selectBoxChanged($event)" label="b2" name="select2" [options]="testObject"></ng2-input-select>

这篇关于如何在窗体内的自定义控件中使用ngModel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆