mgechev / angular2-style-guide

[Deprecated] Community-driven set of best practices and style guidelines for Angular 2 application development
https://mgechev.github.io/angular2-style-guide/
1.2k stars 98 forks source link

Avoid using forwardRef considerations #56

Closed kamarouski closed 8 years ago

kamarouski commented 8 years ago

I didn't find aby mentions of this rule in official styleguide.

https://angular.io/styleguide

There are some cases where we need forwardRef:

  1. Resolve cycle dependencies. I know, I know you tell me it's a bad design, but sometimes we do have them. For example when rendering tree structure of similar components (A renders B, B renders A etc.). It's a valid scenario and cannot be ignored.
  2. ngModel implementation. It's probably just because I didn't find a better way to implement this. Imagine you need implement custom input component that supports ngModel property (e.g. <my-input [(ngModel)]="value" >) To achieve this we need two things:
  3. Implement ControlValueAccessor for our component,
  4. Create custom Control Value Accessor like
export const CSN_MY_INPUT_CONTROL_VALUE_ACCESSOR = new Provider(
    NG_VALUE_ACCESSOR, {
        useExisting: forwardRef(() => MyInputComponent),
        multi: true
    }
);
mgechev commented 8 years ago

Yes, subset of the styles from this style guide moved to the official one after a lot of discussions and meetings. forwardRef is not one of the issues that we agreed to be part of the official style guide.

Since, as mentioned in the readme, this repo is already deprecated, you can join the discussion here https://github.com/angular/angular.io/issues/1151.