填寫這份《一分鐘調查》,幫我們(開發組)做得更好!去填寫Home

DefaultValueAccessor

The default ControlValueAccessor for writing a value and listening to changes on input elements. The accessor is used by the FormControlDirective, FormControlName, and NgModel directives.

檢視"說明"...

Exported from

選擇器

說明

使用預設值訪問器

Using the default value accessor

以下示例示範瞭如何使用輸入元素啟用預設值訪問器(在這種情況下為文字欄位)。

The following example shows how to use an input element that activates the default value accessor (in this case, a text field).

      
      const firstNameControl = new FormControl();
    
      
      <input type="text" [formControl]="firstNameControl">
    

預設情況下,此值訪問器用於 <input type="text"><textarea> 元素,但你也可以將其用於具有類似行為且不需要特殊處理的自訂元件。為了將預設值訪問器附加到自訂元素,請新增 ngDefaultControl 屬性,如下所示。

This value accessor is used by default for <input type="text"> and <textarea> elements, but you could also use it for custom components that have similar behavior and do not require special processing. In order to attach the default value accessor to a custom element, add the ngDefaultControl attribute as shown below.

      
      <custom-input-component ngDefaultControl [(ngModel)]="value"></custom-input-component>