question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Equivalent to react controlled-component

See original GitHub issue

Feature Request

In React, the state be the “single source of truth” in controlled components

but there is no equivalent feature in angular

Relevant Package

@angular/form

Description

DEMO 1:

DEMO 2:

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

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:33
  • Comments:13 (12 by maintainers)

github_iconTop GitHub Comments

10reactions
waterpleacommented, May 18, 2021

[value] is a native property value binding and that’s what it does. It’s not in Angular, it’s in the DOM API 😃 You guys are literally asking for a one-liner to be added to the library. To simplify @petebacondarwin example, here’s the shortest form of this directive:

@Directive({
  selector: 'input[controlled][type="checkbox]',
  host: {
    '(input)': '$event.target.checked = !$event.target.checked'
  }
})
export class ControlledCheckbox {}

Besides being questionable UX, this would probably get you in some trouble from accessibility point of view. This is altering native browser behavior, or rather reverting it which is probably even worse because of Ctrl+Z, caret positioning etc if you would try to use it on something, other than checkboxes.

I would argue this doesn’t belong in the framework.

4reactions
petebacondarwincommented, May 17, 2021

This can be achieved already in a couple of ways. For instance https://stackblitz.com/edit/angular-ivy-wtkqqx?file=src%2Fapp%2Fapp.component.ts

I am not sure that there needs to be any additional change to how Angular input event handling works.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Uncontrolled Components - React
In most cases, we recommend using controlled components to implement forms. In a controlled component, form data is handled by a React component....
Read more >
Controlled vs Uncontrolled Components in React - ITNEXT
In most cases, we recommend using controlled components to implement forms. In a controlled component, form data is handled by a React component....
Read more >
Controlled vs. uncontrolled components in React
In React, there are two ways to handle form data in our components. The first way is by using the state within the...
Read more >
What are Controlled and Uncontrolled Components in React ...
In a controlled component, form data is handled by a React component. Whereas in uncontrolled components, form data is handled by the DOM...
Read more >
The Difference Between Controlled and Uncontrolled ...
When using an uncontrolled component, you can only access the value of the input when you submit the form. However, when using a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found