question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Angular Elements should be able to emit/dispatch an event while initialization

See original GitHub issue

🐞 bug report

Affected Package

The issue is caused by package @angular/elements

Description

We encountered a bug in Angular Elements that the element is not able to emit an event while initializing the component. We tried to emit the event using one of the Angular Component lifecycle hooks (ngOnInit, ngAfterContentInit, ngAfterContentChecked, ngAfterViewInit). Unfortunately, they were not emitted.

🔬 Minimal Reproduction

Here is a simple component example:

@Component({
  selector: 'test-component',
  templateUrl: './test-component.component.html',
  styleUrls: ['./test-component.component.scss']
})
export class TestComponent implements AfterViewInit {
  
	@Output() public connected: EventEmitter<any> = new EventEmitter<any>();

  ngAfterViewInit() {
    this.connected.emit();
  }
}

Component Usage

const comp = document.createElement('test-component');
comp.addEventListener('connected', function(eventParams) {
  console.log('initialized!!!!!'); // will not be called
});
document.body.append(comp);

Live example is available also on stackblitz.com : https://stackblitz.com/edit/angular-issue-repro2-hssxfc

🌍 Your Environment


Angular Version: 6.x & 7.x

Anything else relevant?

I checked the code of the createCustomElement function and found out that the listening to events and dispatching them is achieved after the component is connected! In other words, the statement here: https://github.com/angular/angular/blob/699ecac2c28006df807e240e226e7dbaf9012d3d/packages/elements/src/create-custom-element.ts#L167 is called after this.ngElementStrategy.connect(this); which means that the events will not dispatched as custom events till the ngComponent is initialized.

Moreover, I think it would be great to get a callback/hook when the Web Component is connected to the DOM. Such as componentConnected() hook.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
trotylcommented, May 25, 2019

I noticed in my ngAfterViewInit event the @Inputs() properties are always undefined.

@dimarafonseca That should be https://github.com/angular/angular/issues/27771, doesn’t quite relates to this one.

A better explanation can be found at https://github.com/angular/angular/issues/29050#issuecomment-468786510.

0reactions
angular-automatic-lock-bot[bot]commented, Sep 15, 2019

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular elements overview
Custom elements bootstrap themselves - they start automatically when they are added to the DOM, and are automatically destroyed when removed from the...
Read more >
Understanding the Magic Behind Angular Elements
We want to be able to use the standard Web Components API to set attributes or properties, as well as listen to the...
Read more >
Learn how Angular Elements transmits Component's ...
In the following demo, we have a div that emits a custom event named highlight . When we click the div, it will...
Read more >
Angular + Web Components: a complete guide - InDepth.Dev
In this article you'll know how to incorporate a single Angular component inside ... Of course, it would be great to be able...
Read more >
Building a Custom Element Using Angular Elements
In fact, Angular takes charge of initializing a custom element ... However, these files aren't available at this time on master - so...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found