angular / angular/angular

Equivalent to react controlled-component

Open
#42,126 13 comments 38 reactions 1 assignee Claimed by @alxhub View on GitHub
area: forms core: change detection feature feature: under consideration
Dominant language
TypeScript
Stars
101k
Forks
27.5k
Avg merge
1d 19h
Merged PRs (30d)
288

Description

# Feature Request
In React, the state be the “single source of truth” in [controlled components](https://reactjs.org/docs/forms.html#controlled-components)

but there is no equivalent feature in angular

### Relevant Package
`@angular/form`

### Description

DEMO 1:

- in React

https://stackblitz.com/edit/react-ts-kyqxmj?file=index.tsx

type something in the `input`, and the input value won't change, the state be the single source of truth

- in Angular

https://stackblitz.com/edit/angular-ivy-sazmxc?file=src%2Fapp%2Fapp.component.ts

type something in the `input`, and the input will change, the state won't be the single source of truth

DEMO 2:

- in React

`click` -> `response` -> `true`

the view is always keeping the same as the state

https://stackblitz.com/edit/react-ts-zaolxq?file=index.tsx

- in Angular

`click` -> `true(the checkbox's inner state has changed and not matched with the outer state)` -> `server response` -> `update the inner state to false`

the view is not follow the state when you click it.

https://stackblitz.com/edit/angular-ivy-99wkuf?file=src%2Fapp%2Fapp.component.ts

### Describe the solution you'd like

support changing the form components to use controlled mode or not controlled mode

adding an option to ngModelOptions to support this feature

such as `[ngModelOptions]={controlled: true}`

ref #5184

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.