X Tutup
The Wayback Machine - https://web.archive.org/web/20211208181232/https://github.com/angular/angular/issues/44029
Skip to content
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

Two-Way Binding documentation does not mention getters and setters #44029

Open
CraigComeOnThisNameCantBeTaken opened this issue Nov 3, 2021 · 3 comments

Comments

@CraigComeOnThisNameCantBeTaken
Copy link

@CraigComeOnThisNameCantBeTaken CraigComeOnThisNameCantBeTaken commented Nov 3, 2021

Description

You can use getters and setters with a single @input with getters and setters to achieve the same result as with @output and EventEmitters as below:

  private _childItem: any;

  @Input()
  get childItem() {
    return this._childItem;
  }
  set childItem(val) {
    this._childItem = val;
  }

  ngOnInit() {
    this.childItem = 'hello'; // whether I set the private field or the public setter I get the same result
  }

Would it be possible to explain why this is a worse approach than EventEmitters please?

What is the affected URL?

https://angular.io/guide/two-way-binding

Please provide the steps to reproduce the issue

No response

Please provide the expected behavior vs the actual behavior you encountered

No response

Please provide a screenshot if possible

No response

Please provide the exception or error you saw

No response

Is this a browser-specific issue? If so, please specify the device, browser, and version.

No response

@JoostK
Copy link
Member

@JoostK JoostK commented Nov 3, 2021

I don't understand the question. Your example does not include any EventEmitter, could you please clarify the situation?

Loading

@ngbot ngbot bot added this to the Backlog milestone Nov 3, 2021
@CraigComeOnThisNameCantBeTaken
Copy link
Author

@CraigComeOnThisNameCantBeTaken CraigComeOnThisNameCantBeTaken commented Nov 4, 2021

I mean that the code I have written above seems to work in the same way as an event emitter. It still updates the parent component with the new value.

Loading

@JoostK
Copy link
Member

@JoostK JoostK commented Nov 4, 2021

There's ony one component in your example, so I don't see the relation to a parent component.

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
X Tutup