angular/packages
Kristiyan Kostadinov 68017d4e75 feat(core): add ability to transform input values (#50420)
According to the HTML specification most attributes are defined as strings, however some can be interpreted as different types like booleans or numbers. [In the HTML standard](https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#boolean-attributes), boolean attributes are considered `true` if they are present on a DOM node and `false` if they are omitted. Common examples of boolean attributes are `disabled` on interactive elements like `<button>` or `checked` on `<input type="checkbox">`. Another example of an attribute that is defined as a string, but interpreted as a different type is the `value` attribute of `<input type="number">` which logs a warning and ignores the value if it can't be parsed as a number.

Historically, authoring Angular inputs that match the native behavior in a type-safe way has been difficult for developers, because Angular interprets all static attributes as strings. While some recent TypeScript versions made this easier by allowing setters and getters to have different types, supporting this pattern still requires a lot of boilerplate and additional properties to be declared. For example, currently developers have to write something like this to have a `disabled` input that behaves like the native one:

```typescript
import {Directive, Input} from '@angular/core';

@Directive({selector: 'mat-checkbox'})
export class MatCheckbox {
  @Input()
  get disabled() {
    return this._disabled;
  }
  set disabled(value: any) {
    this._disabled = typeof value === 'boolean' ? value : (value != null && value !== 'false');
  }
  private _disabled = false;
}
```

This feature aims to address the issue by introducing a `transform` property on inputs. If an input has a `transform` function, any values set through the template will be passed through the function before being assigned to the directive instance. The example from above can be rewritten to the following:

```typescript
import {Directive, Input, booleanAttribute} from '@angular/core';

@Directive({selector: 'mat-checkbox'})
export class MatCheckbox {
  @Input({transform: booleanAttribute}) disabled: boolean = false;
}
```

These changes also add the `booleanAttribute` and `numberAttribute` utilities to `@angular/core` since they're common enough to be useful for most projects.

Fixes #8968.
Fixes #14761.

PR Close #50420
2023-05-30 13:01:13 -07:00
..
animations fix(animations): Trigger leave animation when ViewContainerRef is injected (#48705) 2023-05-25 14:40:34 -04:00
bazel build: update dependency @rollup/plugin-commonjs to v25 (#50264) 2023-05-12 10:15:11 -07:00
benchpress docs: removed the unused import and update the obsolete tag of width in table tag (#50474) 2023-05-30 09:23:06 -07:00
common refactor(http): replace zone.js macrotask creation with InitialRenderPendingTasks (#50425) 2023-05-30 12:58:22 -07:00
compiler feat(core): add ability to transform input values (#50420) 2023-05-30 13:01:13 -07:00
compiler-cli feat(core): add ability to transform input values (#50420) 2023-05-30 13:01:13 -07:00
core feat(core): add ability to transform input values (#50420) 2023-05-30 13:01:13 -07:00
docs/di docs: remove outdated/unsupported webworker doc (#49856) 2023-04-17 14:01:41 +00:00
elements refactor(elements): remove unnecessary polyfill deps (#50115) 2023-05-10 14:32:36 -07:00
examples docs: ngComponentOutlet doc with projected template (#50173) 2023-05-19 16:35:48 +00:00
forms feat(core): add ability to transform input values (#50420) 2023-05-30 13:01:13 -07:00
language-service feat(core): support TypeScript 5.1 (#50156) 2023-05-09 14:44:30 -07:00
localize refactor(localize): run spell check on localize package (#50445) 2023-05-24 13:56:55 +00:00
misc/angular-in-memory-web-api build: bump in-memory-web-api dependency versions (#50246) 2023-05-11 14:26:02 -07:00
platform-browser fix(animations): Trigger leave animation when ViewContainerRef is injected (#48705) 2023-05-25 14:40:34 -04:00
platform-browser-dynamic refactor(platform-browser): cleanup references to dart (#49858) 2023-04-18 14:00:45 +00:00
platform-server fix(core): update ApplicationRef.isStable to account for rendering pending tasks (#50425) 2023-05-30 12:58:22 -07:00
private/testing fix(platform-server): bundle @angular/domino in via esbuild (#49229) 2023-02-28 13:34:50 -08:00
router feat(core): add ability to transform input values (#50420) 2023-05-30 13:01:13 -07:00
service-worker build: update minimum supported Node version from 16.13.0 -> 16.14.0 (#49771) 2023-04-11 07:56:31 -07:00
upgrade build: update minimum supported Node version from 16.13.0 -> 16.14.0 (#49771) 2023-04-11 07:56:31 -07:00
zone.js fix(zone.js): enable monkey patching of the queueMicrotask() API in node.js (#50467) 2023-05-30 12:57:45 -07:00
BUILD.bazel build(bazel): list docs entrypoints under packages to simplify docgen 2022-11-22 13:51:16 -07:00
circular-deps-test.conf.js build: ignore type only imports/exports for circular dependency checks (#47017) 2022-08-18 12:06:09 -07:00
empty.ts build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00
goog.d.ts refactor(core): replace references to goog.LOCALE with goog.getLocale() (#43089) 2021-08-10 10:35:26 -07:00
license-banner.txt build: bump license year (#44590) 2022-01-04 12:05:25 -08:00
README.md
system.d.ts build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00
tsconfig-build.json build: update ts_library to produce ES2022 (#49559) 2023-03-23 08:18:45 -07:00
tsconfig-legacy-saucelabs.json refactor(compiler): drop obsolete NgFactory and NgSummary config options (#48268) 2023-02-21 13:03:59 -08:00
tsconfig-test.json build: switch devmode output to es2015 (#44505) 2022-01-05 23:20:20 +00:00
tsconfig-tsec-base.json build: update tsconfigs to use ES2020 as target and module (#43431) 2021-10-01 18:28:42 +00:00
tsconfig.json refactor(compiler): drop obsolete NgFactory and NgSummary config options (#48268) 2023-02-21 13:03:59 -08:00
tsec-exemption.json fix(core): hardening attribute and property binding rules for <iframe> elements (#47964) 2022-11-09 00:47:56 -08:00
types.d.ts build: update to jasmine 4.0 (#45558) 2022-04-11 16:25:28 +00:00

Angular

The sources for this package are in the main Angular repo. Please file issues and pull requests against that repo.

Usage information and reference details can be found in Angular documentation.

License: MIT