Deliver web apps with confidence 🚀
Find a file
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
.circleci build: don't run saucelabs tests yet on PRs... that will happen in a followup (#49200) 2023-05-15 09:21:46 -07:00
.devcontainer build: Fix version of images in recommended-Dockerfile. node:14-browsers doesn't exist anymore in Dockerhub (#48803) 2023-01-24 16:38:32 +00:00
.github build: update github/codeql-action action to v2.3.3 (#50216) 2023-05-09 14:48:54 -07:00
.husky build: remove unnecessary husky gitignore file (#42870) 2021-07-19 17:31:55 -07:00
.ng-dev build: remove the webworker platform. (#50059) 2023-05-04 09:06:55 +02:00
.vscode build: update recommended launch vscode config to use proper bazel dist path (#47831) 2022-10-24 10:42:21 +02:00
.yarn build: update to yarn v1.22.17 (#44150) 2021-11-15 11:19:05 -08:00
aio docs: removed the obsolete frameborder Tag and updated the files with using css (#50480) 2023-05-30 12:59:26 -07:00
devtools docs: remove unused imports and improved the code (#50424) 2023-05-25 14:37:54 +00:00
docs docs: update CLI command to include --preserve-symlinks-main (#50512) 2023-05-30 09:20:53 -07:00
goldens feat(core): add ability to transform input values (#50420) 2023-05-30 13:01:13 -07:00
integration docs: improved code quality (#50443) 2023-05-30 13:00:39 -07:00
modules refactor: remove benchpres tests for other frameworks (#50108) 2023-05-10 14:29:10 -07:00
packages feat(core): add ability to transform input values (#50420) 2023-05-30 13:01:13 -07:00
scripts build: remove unused script from removed components CI job (#50429) 2023-05-23 17:18:27 +00:00
third_party build: create NodeJS ESM loader for supporting Bazel setup (#48521) 2022-12-19 19:50:40 +00:00
tools refactor(devtools): run spell check on tools dir (#50445) 2023-05-24 13:56:56 +00:00
.bazelignore feat(core): support TypeScript 5.1 (#50156) 2023-05-09 14:44:30 -07:00
.bazelrc build: avoid unnecessary re-evaluation of starlark code (#49106) 2023-02-16 15:31:55 -08:00
.bazelversion build: update to bazel v5 for new runfiles API used in dev-infra (#45407) 2022-03-21 16:55:36 -07:00
.clang-format feat(tooling): Add a .clang-format for automated JavaScript formatting. 2015-04-02 08:44:34 -07:00
.editorconfig build: use https link to editorconfig.org in .editorconfig (#27664) 2018-12-18 09:30:09 -08:00
.gitattributes build: cleanup .gitattributes file and remove outdated CRLF attribute (#46513) 2022-06-28 13:38:27 -07:00
.gitignore docs: update of landing page and backup pages (#47047) 2022-12-09 16:04:34 -08:00
.gitmessage build: clean up references to old master branch (#45856) 2022-05-04 16:23:33 -07:00
.mailmap build: add a Git .mailmap with my new name (#19550) 2017-10-09 14:35:30 -07:00
.npmrc build: rely on engines to prevent using npm for dependency install (#41477) 2021-04-07 12:05:01 -07:00
.nvmrc build: update minimum supported Node version from 16.13.0 -> 16.14.0 (#49771) 2023-04-11 07:56:31 -07:00
.prettierrc ci: use prettier to format yml files (#42136) 2021-05-19 11:20:20 -07:00
.pullapprove.yml build: share Saucelabs browsers between karma test targets using background Saucelabs daemon and custom karma launcher (#49200) 2023-05-15 09:21:46 -07:00
.yarnrc build: update to yarn v1.22.17 (#44150) 2021-11-15 11:19:05 -08:00
browser-providers.conf.d.ts build: share Saucelabs browsers between karma test targets using background Saucelabs daemon and custom karma launcher (#49200) 2023-05-15 09:21:46 -07:00
browser-providers.conf.js ci: enable Android 12 for tests on CI (#45926) 2022-05-09 11:26:10 -07:00
BUILD.bazel build: share Saucelabs browsers between karma test targets using background Saucelabs daemon and custom karma launcher (#49200) 2023-05-15 09:21:46 -07:00
CHANGELOG.md release: cut the v16.1.0-next.2 release 2023-05-24 11:03:56 -07:00
CHANGELOG_ARCHIVE.md docs: archiving older changelog entries (#49957) 2023-05-02 12:10:11 +02:00
CODE_OF_CONDUCT.md build: update CODE_OF_CONDUCT.md to match the content of angular/.github 2023-03-13 21:07:51 +00:00
CONTRIBUTING.md refactor: remove Angular Compatibility Compiler (ngcc) (#49101) 2023-02-16 16:01:17 -08:00
gulpfile.js build: remove the unused source-map-test (#46518) 2022-06-27 15:42:43 -07:00
karma-js.conf.js build: share Saucelabs browsers between karma test targets using background Saucelabs daemon and custom karma launcher (#49200) 2023-05-15 09:21:46 -07:00
LICENSE build: bump license year (#49984) 2023-04-25 09:29:16 -07:00
package.json build: update dependency https-proxy-agent to v7 (#50456) 2023-05-25 14:40:05 +00:00
packages.bzl docs: Add rxjs-interop and reactivity primitives APIs (#50066) 2023-05-01 07:53:47 -07:00
README.md docs: update README.md (#48423) 2023-04-18 16:59:38 +00:00
renovate.json ci: update renovate.json (#49909) 2023-04-18 20:54:28 +00:00
SECURITY.md build: update SECURITY.md to match the content of angular/.github 2023-01-06 08:51:29 +00:00
tsconfig-tslint.json refactor(devtools): run ng-dev format on angular devtools files 2022-01-26 16:35:30 -05:00
tslint.json build: update lint path for @angular/build-tooling rules (#49266) 2023-02-28 11:56:28 -08:00
WORKSPACE build: share Saucelabs browsers between karma test targets using background Saucelabs daemon and custom karma launcher (#49200) 2023-05-15 09:21:46 -07:00
yarn.bzl build(bazel): incrementally run aio example e2e tests 2022-11-22 13:51:16 -07:00
yarn.lock build: update dependency https-proxy-agent to v7 (#50456) 2023-05-25 14:40:05 +00:00
yarn.lock.readme.md build: clean up references to old master branch (#45856) 2022-05-04 16:23:33 -07:00

Angular - The modern web developer's platform

angular-logo
Angular is a development platform for building mobile and desktop web applications
using TypeScript/JavaScript and other languages.

www.angular.io

Contributing Guidelines · Submit an Issue · Blog

CI status   Angular on npm   Discord conversation

InsightsSnapshot


Documentation

Get started with Angular, learn the fundamentals and explore advanced topics on our documentation website.

Advanced

Development Setup

Prerequisites

Setting Up a Project

Install the Angular CLI globally:

npm install -g @angular/cli

Create workspace:

ng new [PROJECT NAME]

Run the application:

cd [PROJECT NAME]
ng serve

Angular is cross-platform, fast, scalable, has incredible tooling, and is loved by millions.

Quickstart

Get started in 5 minutes.

Ecosystem

angular ecosystem logos

Changelog

Learn about the latest improvements.

Upgrading

Check out our upgrade guide to find out the best way to upgrade your project.

Contributing

Contributing Guidelines

Read through our contributing guidelines to learn about our submission process, coding rules, and more.

Want to Help?

Want to report a bug, contribute some code, or improve the documentation? Excellent! Read up on our guidelines for contributing and then check out one of our issues labeled as help wanted or good first issue.

Code of Conduct

Help us keep Angular open and inclusive. Please read and follow our Code of Conduct.

Community

Join the conversation and help the community.

Love Angular badge

Love Angular? Give our repo a star ⬆️.