-
Notifications
You must be signed in to change notification settings - Fork 6.8k
docs(cdk/coercion): expand overview #22791
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,59 @@ | ||
### Coercion | ||
|
||
Utility functions for coercing `@Input`s into specific types. | ||
|
||
### Example | ||
|
||
```ts | ||
import {Directive, ElementRef, HostListener} from '@angular/core'; | ||
import { | ||
coerceBooleanProperty, | ||
BooleanInput, | ||
NumberInput, | ||
coerceNumberProperty, | ||
coerceElement, | ||
} from '@angular/cdk/coercion'; | ||
|
||
@Directive({ | ||
selector: 'my-button', | ||
host: { | ||
'[disabled]': 'disabled', | ||
'(click)': 'greet()', | ||
} | ||
}) | ||
class MyButton { | ||
// Using `coerceBooleanProperty` allows for the disabled value of a button to be set as | ||
// `<my-button disabled></my-button>` instead of `<my-button [disabled]="true"></my-button>`. | ||
// It also allows for a string to be passed like `<my-button disabled="true"></my-button>`. | ||
@Input() | ||
get disabled() { return this._disabled; } | ||
set disabled(value: any) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry to ask, but is |
||
this._disabled = coerceBooleanProperty(value); | ||
} | ||
private _disabled = false; | ||
|
||
// `coerceNumberProperty` turns any value coming in from the view into a number, allowing the | ||
// consumer to use a shorthand string while storing the parsed number in memory. E.g. the consumer can write: | ||
// `<my-button greetDelay="500"></my-button>` instead of `<my-button [greetDelay]="500"></my-button>`. | ||
crisbeto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// The second parameter specifies a fallback value to be used if the value can't be | ||
// parsed to a number. | ||
@Input() | ||
get greetDelay() { return this._greetDelay; } | ||
set greetDelay(value: any) { | ||
this._greetDelay = coerceNumberProperty(value, 0); | ||
} | ||
private _greetDelay = 0; | ||
|
||
greet() { | ||
setTimeout(() => alert('Hello!'), this.greetDelay); | ||
} | ||
|
||
// `coerceElement` allows you to accept either an `ElementRef` | ||
// or a raw DOM node and to always return a DOM node. | ||
getElement(elementOrRef: ElementRef<HTMLElement> | HTMLElement): HTMLElement { | ||
return coerceElement(elementOrRef); | ||
} | ||
|
||
// Required so that the template type checker can infer the type of the coerced inputs. | ||
static ngAcceptInputType_disabled: BooleanInput; | ||
static ngAcceptInputType_greetDelay: NumberInput; | ||
} | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we make this a full example? It's pretty extensive for a code block
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My reasoning for keeping it inline is that the example won't be particularly visual since the coercion only affects the public API of the component.