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

@Directive @Input('<') setter called before constructor #102

Closed
kylekatarnls opened this issue May 23, 2019 · 5 comments
Closed

@Directive @Input('<') setter called before constructor #102

kylekatarnls opened this issue May 23, 2019 · 5 comments

Comments

@kylekatarnls
Copy link

Hello,

I created a directive and noticed that @Input setter was called before the class to be constructed.

This prevents to use injected dependencies in those setters.

import {Directive, Input} from 'angular-ts-decorators';

@Directive({
    selector: 'foo-disabled',
})
export class FooDisabledDirective {
    @Input('<') public set fooDisabled(disabled: boolean) {
        console.log('fooDisabled', this.$element);
    }

    constructor(private $element: ng.IAugmentedJQuery) {
        console.log('construct', $element);
    }
}
<div foo-disabled="5 == 5"></div>

Observed in console:

fooDisabled, undefined
construct, <div foo-disabled="5 == 5"></div>

Expected:

construct, <div foo-disabled="5 == 5"></div>
fooDisabled, <div foo-disabled="5 == 5"></div>

Expected order is the one we get in Angular 2+.

In my opinion, this is a bug. I'm not sure to be able to fix it but I can try to make a pull-request if you're agree to fix it.

Thanks.

@vsternbach
Copy link
Owner

vsternbach commented May 23, 2019

You shouldn't be accessing $element in constructor according to angular, it should be done in ngOnInit hook

@kylekatarnls
Copy link
Author

$element is not the problem, it would be the same with a service it won't be defined in the setter because the setter is called before the constructor. This is a non-sense. The constructor of a class should be called before any other method. because if the instance does not yet exists, I should be able to call any method.

Replace $element with $filter in the example above, and you'll get the same inconsistency: this.$filter is undefined because the setter is called before the constructor.

@vsternbach
Copy link
Owner

vsternbach commented May 23, 2019 via email

@kylekatarnls
Copy link
Author

I'm in the worst case (1.5.11):

In 1.5.10+, the default behavior will be to assign before calling the constructor, but you can change it to delay until before $onInit.

Indeed we should upgrade.

Thanks!

@kylekatarnls
Copy link
Author

Thanks again for your quick response. We really love your library. ❤️

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

No branches or pull requests

2 participants