Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Directive's controller setter called before class instantiation (view attributes assigned before initialization) #13590

Closed
@CodySchaaf

Description

@CodySchaaf

This seems to be intentional in order to allow access to bound variables during a directive's controller instantiation, but it is really unintuitive.

As you can see in this codepen this can cause issues with variables that should have been instantiated in the constructor not being available when a method is called.

Basically in my situation we have a controller that needs to notify its link function when a bound variable is changed (widget.title), to do this I set up a setter for the variable that calls notify when it is called.

So the parent directive can change the title, then the setter is called which notifies the widget's link function. Although since the parentDir changes the title in it's constructor we get a console error where notify is called before the array of subscribers is set up. You can uncomment the code to see the order of the console logs. This code would allow my version to work, although it would end up masking other issues.

The code responsible for this is here

Following this pattern:

var instance = Object.create(directiveDefinition.controller.prototype);

instance.widgetTitle = "New Title"

directiveDefinition.controller.apply(instance);

Is there an existing pattern for getting around this issue? Seems sort of like a bug, although it would probably require a pretty big breaking change that would make a lot of people angry.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions