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

Can I use injected dependencies in a hook? #160

Closed
31piy opened this issue Mar 28, 2020 · 3 comments · Fixed by #188
Closed

Can I use injected dependencies in a hook? #160

31piy opened this issue Mar 28, 2020 · 3 comments · Fixed by #188
Assignees
Labels
flag: duplicate This issue or PR is a duplicate released on @feat-rewrite released state: has PR A PR is available for that issue type: RFC/discussion/question This issue is about RFC, discussion or a question

Comments

@31piy
Copy link

31piy commented Mar 28, 2020

This is more of a question than an issue. I've a use-case where I want to supply the default value of a form control using the injected dependencies in the component:

import { TranslateService } from '@ngx-translate/core';
import { NgxSubFormComponent } from 'ngx-sub-form';

export class MyComponent extends NgxSubFormComponent<MyForm> implements OnInit {
  constructor(private translate: TranslateService) {...}

  getDefaultValues() {
    return {
      name: this.translate.instant('my.translate.key')
    };
  }

  // Rest of the implementation.
}

This obviously fails as it isn't finding the translate dependency when getDefaultValues is being called. I've observed that this method is called before and after the component initialization. So if I change the logic as the follows, it works fine:

  getDefaultValues() {
    return {
      name: this.translate ? this.translate.instant('my.translate.key') : ''
    };
  }

I'm not sure whether there is a standard way of achieving this. Any help is appreciated. Thanks in advance.

@maxime1992
Copy link
Contributor

Hi @31piy 👋,

there's a similar issue here: #82

I made a PR to not have that limitation but it brought a few weird things too (CF thread).

The only thing I can think of in your case is to "abuse" the transformToFormGroup method and apply your logic there to return the new object before it's applied to the form

@maxime1992 maxime1992 added the type: RFC/discussion/question This issue is about RFC, discussion or a question label Mar 28, 2020
maxime1992 added a commit that referenced this issue Jun 15, 2020
This is a major architecture change which is brought without any breaking change 😄!

We've split up the code base in 2: Old one and new one.
The old one hasn't moved at all but is now deprecated (not removed yet!).
You can keep using the old one for a bit and have a smooth/incremental update to use the new API.

Few changes that you have to note with the new API:
- Only works with Angular 9 or more
- The app needs to have Ivy activated (this is because we use `ɵmarkDirty` internally. If it ever gets removed we'll probably have to ask to provide the `ChangeDetectorRef` but we were able to around this for now!)
- We got rid of inheritance 🙌
- Form errors on a FormArray are now an object instead of an array. Previously the array contained null values on all the fields without any error. It's now an object containing only the ones with errors and you can access them using the index

Please start upgrading to the new API as soon as possible as we stop supporting the old API as of today and will remove it in a near release.

This closes #171 for the major architectural changes and also the following issues as a result:
- closes #82
- closes #86
- closes #93
- closes #133
- closes #143
- closes #144
- closes #149
- closes #160
- closes #168
@maxime1992 maxime1992 added state: has PR A PR is available for that issue flag: duplicate This issue or PR is a duplicate labels Jun 15, 2020
@maxime1992 maxime1992 self-assigned this Jun 15, 2020
@maxime1992
Copy link
Contributor

🎉 This issue has been resolved in version 5.2.0-feat-rewrite.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

@maxime1992 maxime1992 linked a pull request Oct 11, 2020 that will close this issue
zakhenry pushed a commit that referenced this issue Oct 23, 2020
This is a major architecture change which is brought without any breaking change 😄!

We've split up the code base in 2: Old one and new one.
The old one hasn't moved at all but is now deprecated (not removed yet!).
You can keep using the old one for a bit and have a smooth/incremental update to use the new API.

Few changes that you have to note with the new API:
- Only works with Angular 9 or more
- The app needs to have Ivy activated (this is because we use `ɵmarkDirty` internally. If it ever gets removed we'll probably have to ask to provide the `ChangeDetectorRef` but we were able to around this for now!)
- We got rid of inheritance 🙌
- Form errors on a FormArray are now an object instead of an array. Previously the array contained null values on all the fields without any error. It's now an object containing only the ones with errors and you can access them using the index

Please start upgrading to the new API as soon as possible as we stop supporting the old API as of today and will remove it in a near release.

This closes #171 for the major architectural changes and also the following issues as a result:
- closes #82
- closes #86
- closes #93
- closes #133
- closes #143
- closes #144
- closes #149
- closes #160
- closes #168
maxime1992 added a commit that referenced this issue Nov 21, 2021
This is a major architecture change which is brought without any breaking change 😄!

We've split up the code base in 2: Old one and new one.
The old one hasn't moved at all but is now deprecated (not removed yet!).
You can keep using the old one for a bit and have a smooth/incremental update to use the new API.

Few changes that you have to note with the new API:
- Only works with Angular 9 or more
- The app needs to have Ivy activated (this is because we use `ɵmarkDirty` internally. If it ever gets removed we'll probably have to ask to provide the `ChangeDetectorRef` but we were able to around this for now!)
- We got rid of inheritance 🙌
- Form errors on a FormArray are now an object instead of an array. Previously the array contained null values on all the fields without any error. It's now an object containing only the ones with errors and you can access them using the index

Please start upgrading to the new API as soon as possible as we stop supporting the old API as of today and will remove it in a near release.

This closes #171 for the major architectural changes and also the following issues as a result:
- closes #82
- closes #86
- closes #93
- closes #133
- closes #143
- closes #144
- closes #149
- closes #160
- closes #168
@github-actions
Copy link

🎉 This issue has been resolved in version 5.2.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flag: duplicate This issue or PR is a duplicate released on @feat-rewrite released state: has PR A PR is available for that issue type: RFC/discussion/question This issue is about RFC, discussion or a question
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants