/
githubmirror
/
angular
Обзор
Документация
Войти
/
githubmirror
/
angular
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
packages/forms/src/directives/checkbox_value_accessor.ts
64 строки
2 KB
Alex Rickabaugh
feat(forms): support ngNoCva as an opt-out for ControlValueAccessors
17 мар 2026, 22:18
17 мар 2026, 22:18
3983080
Код
Авторство
О чём код?
/** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.dev/license */ import {Directive, forwardRef, Provider} from '@angular/core'; import { BuiltInControlValueAccessor, ControlValueAccessor, NG_VALUE_ACCESSOR, } from './control_value_accessor'; const CHECKBOX_VALUE_ACCESSOR: Provider = { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => CheckboxControlValueAccessor), multi: true, }; /** * @description * A `ControlValueAccessor` for writing a value and listening to changes on a checkbox input * element. * * @usageNotes * * ### Using a checkbox with a reactive form. * * The following example shows how to use a checkbox with a reactive form. * * ```ts * const rememberLoginControl = new FormControl(); * ``` * * ```html * <input type="checkbox" [formControl]="rememberLoginControl"> * ``` * * @ngModule ReactiveFormsModule * @ngModule FormsModule * @publicApi */ @Directive({ selector: 'input[type=checkbox]:not([ngNoCva])[formControlName],input[type=checkbox]:not([ngNoCva])[formControl],input[type=checkbox]:not([ngNoCva])[ngModel]', host: {'(change)': 'onChange($any($event.target).checked)', '(blur)': 'onTouched()'}, providers: [CHECKBOX_VALUE_ACCESSOR], standalone: false, }) export class CheckboxControlValueAccessor extends BuiltInControlValueAccessor implements ControlValueAccessor { /** * Sets the "checked" property on the input element. * @docs-private */ writeValue(value: any): void { this.setProperty('checked', value); } }