Skip to content

Commit

Permalink
fix(elements): always check to create strategy (#23825)
Browse files Browse the repository at this point in the history
PR Close #23825
  • Loading branch information
andrewseguin authored and IgorMinar committed May 10, 2018
1 parent c4221da commit b1cda36
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion packages/elements/src/create-custom-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,29 @@ export function createCustomElement<P>(

constructor(injector?: Injector) {
super();

// Note that some polyfills (e.g. document-register-element) do not call the constructor.
// Do not assume this strategy has been created.
// TODO(andrewseguin): Add e2e tests that cover cases where the constructor isn't called. For
// now this is tested using a Google internal test suite.
this.ngElementStrategy = strategyFactory.create(injector || config.injector);
}

attributeChangedCallback(
attrName: string, oldValue: string|null, newValue: string, namespace?: string): void {
if (!this.ngElementStrategy) {
this.ngElementStrategy = strategyFactory.create(config.injector);
}

const propName = attributeToPropertyInputs[attrName] !;
this.ngElementStrategy.setInputValue(propName, newValue);
}

connectedCallback(): void {
if (!this.ngElementStrategy) {
this.ngElementStrategy = strategyFactory.create(config.injector);
}

this.ngElementStrategy.connect(this);

// Listen for events from the strategy and dispatch them as custom events
Expand All @@ -155,7 +168,9 @@ export function createCustomElement<P>(
}

disconnectedCallback(): void {
this.ngElementStrategy.disconnect();
if (this.ngElementStrategy) {
this.ngElementStrategy.disconnect();
}

if (this.ngElementEventsSubscription) {
this.ngElementEventsSubscription.unsubscribe();
Expand Down

0 comments on commit b1cda36

Please sign in to comment.