angular-cn/packages/forms/src/directives/control_value_accessor.ts

119 lines
3.3 KiB
TypeScript
Raw Normal View History

/**
* @license
* Copyright Google Inc. 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.io/license
*/
feat(core): Add type information to injector.get() (#13785) - Introduce `InjectionToken<T>` which is a parameterized and type-safe version of `OpaqueToken`. DEPRECATION: - `OpaqueToken` is now deprecated, use `InjectionToken<T>` instead. - `Injector.get(token: any, notFoundValue?: any): any` is now deprecated use the same method which is now overloaded as `Injector.get<T>(token: Type<T>|InjectionToken<T>, notFoundValue?: T): T;`. Migration - Replace `OpaqueToken` with `InjectionToken<?>` and parameterize it. - Migrate your code to only use `Type<?>` or `InjectionToken<?>` as injection tokens. Using other tokens will not be supported in the future. BREAKING CHANGE: - Because `injector.get()` is now parameterize it is possible that code which used to work no longer type checks. Example would be if one injects `Foo` but configures it as `{provide: Foo, useClass: MockFoo}`. The injection instance will be that of `MockFoo` but the type will be `Foo` instead of `any` as in the past. This means that it was possible to call a method on `MockFoo` in the past which now will fail type check. See this example: ``` class Foo {} class MockFoo extends Foo { setupMock(); } var PROVIDERS = [ {provide: Foo, useClass: MockFoo} ]; ... function myTest(injector: Injector) { var foo = injector.get(Foo); // This line used to work since `foo` used to be `any` before this // change, it will now be `Foo`, and `Foo` does not have `setUpMock()`. // The fix is to downcast: `injector.get(Foo) as MockFoo`. foo.setUpMock(); } ``` PR Close #13785
2017-01-03 16:54:46 -08:00
import {InjectionToken} from '@angular/core';
2016-06-08 15:36:24 -07:00
/**
* A `ControlValueAccessor` acts as a bridge between the Angular forms API and a
* native element in the DOM.
2016-06-08 15:36:24 -07:00
*
* Implement this interface if you want to create a custom form control directive
* that integrates with Angular forms.
2016-06-08 15:36:24 -07:00
*
* @stable
2016-06-08 15:36:24 -07:00
*/
export interface ControlValueAccessor {
/**
* Writes a new value to the element.
*
* This method will be called by the forms API to write to the view when programmatic
* (model -> view) changes are requested.
*
* Example implementation of `writeValue`:
*
* ```ts
* writeValue(value: any): void {
* this._renderer.setProperty(this._elementRef.nativeElement, 'value', value);
* }
* ```
2016-06-08 15:36:24 -07:00
*/
writeValue(obj: any): void;
/**
* Registers a callback function that should be called when the control's value
* changes in the UI.
*
* This is called by the forms API on initialization so it can update the form
* model when values propagate from the view (view -> model).
*
* If you are implementing `registerOnChange` in your own value accessor, you
* will typically want to save the given function so your class can call it
* at the appropriate time.
*
* ```ts
* registerOnChange(fn: (_: any) => void): void {
* this._onChange = fn;
* }
* ```
*
* When the value changes in the UI, your class should call the registered
* function to allow the forms API to update itself:
*
* ```ts
* host: {
* (change): '_onChange($event.target.value)'
* }
* ```
*
2016-06-08 15:36:24 -07:00
*/
registerOnChange(fn: any): void;
/**
* Registers a callback function that should be called when the control receives
* a blur event.
*
* This is called by the forms API on initialization so it can update the form model
* on blur.
*
* If you are implementing `registerOnTouched` in your own value accessor, you
* will typically want to save the given function so your class can call it
* when the control should be considered blurred (a.k.a. "touched").
*
* ```ts
* registerOnTouched(fn: any): void {
* this._onTouched = fn;
* }
* ```
*
* On blur (or equivalent), your class should call the registered function to allow
* the forms API to update itself:
*
* ```ts
* host: {
* '(blur)': '_onTouched()'
* }
* ```
2016-06-08 15:36:24 -07:00
*/
registerOnTouched(fn: any): void;
/**
* This function is called by the forms API when the control status changes to
* or from "DISABLED". Depending on the value, it should enable or disable the
* appropriate DOM element.
*
* Example implementation of `setDisabledState`:
*
* ```ts
* setDisabledState(isDisabled: boolean): void {
* this._renderer.setProperty(this._elementRef.nativeElement, 'disabled', isDisabled);
* }
* ```
*
* @param isDisabled
*/
setDisabledState?(isDisabled: boolean): void;
2016-06-08 15:36:24 -07:00
}
/**
* Used to provide a {@link ControlValueAccessor} for form controls.
*
* See {@link DefaultValueAccessor} for how to implement one.
* @stable
2016-06-08 15:36:24 -07:00
*/
feat(core): Add type information to injector.get() (#13785) - Introduce `InjectionToken<T>` which is a parameterized and type-safe version of `OpaqueToken`. DEPRECATION: - `OpaqueToken` is now deprecated, use `InjectionToken<T>` instead. - `Injector.get(token: any, notFoundValue?: any): any` is now deprecated use the same method which is now overloaded as `Injector.get<T>(token: Type<T>|InjectionToken<T>, notFoundValue?: T): T;`. Migration - Replace `OpaqueToken` with `InjectionToken<?>` and parameterize it. - Migrate your code to only use `Type<?>` or `InjectionToken<?>` as injection tokens. Using other tokens will not be supported in the future. BREAKING CHANGE: - Because `injector.get()` is now parameterize it is possible that code which used to work no longer type checks. Example would be if one injects `Foo` but configures it as `{provide: Foo, useClass: MockFoo}`. The injection instance will be that of `MockFoo` but the type will be `Foo` instead of `any` as in the past. This means that it was possible to call a method on `MockFoo` in the past which now will fail type check. See this example: ``` class Foo {} class MockFoo extends Foo { setupMock(); } var PROVIDERS = [ {provide: Foo, useClass: MockFoo} ]; ... function myTest(injector: Injector) { var foo = injector.get(Foo); // This line used to work since `foo` used to be `any` before this // change, it will now be `Foo`, and `Foo` does not have `setUpMock()`. // The fix is to downcast: `injector.get(Foo) as MockFoo`. foo.setUpMock(); } ``` PR Close #13785
2017-01-03 16:54:46 -08:00
export const NG_VALUE_ACCESSOR = new InjectionToken<ControlValueAccessor>('NgValueAccessor');