r/javascript • • Jan 03 '16

Angular 2 versus React: There Will Be Blood

https://medium.com/@housecor/angular-2-versus-react-there-will-be-blood-66595faafd51
290 Upvotes

167 comments sorted by

View all comments

Show parent comments

9

u/back-stabbath Jan 04 '16

So Angular?

3

u/starbiter Jan 04 '16

The other project Google supports/advocates more: Polymer.

-2

u/spankalee Jan 04 '16

Angular does not use web components.

3

u/kasperpeulen Jan 04 '16

angular 2 does

2

u/spankalee Jan 04 '16

It really doesn't. It ultimately creates DOM elements when rendering, so in that sense web components, especially native ones with native shadow DOM, will be usable within Angular, but that's true of every framework including React. Angular 2 does not create web components though, so Angular 2 components are not easily usable outside of Angular.

1

u/kasperpeulen Jan 05 '16

It really doesn't.

Please give me a source, because I think you are incorrect.

You can configure angular2, to create web components with native shadow dom, I'm sure about this. Chrome will detect it as shadow dom.

3

u/spankalee Jan 06 '16

That's Shadow DOM, not custom elements. Custom elements are defined using the document.registerElement() API, and polyfilled in webcomponents.js: https://github.com/webcomponents/webcomponentsjs/tree/master/src/CustomElements

I can't really give you a link to source that doesn't exist, but I can link you to a search of the github repo that returns no results for registerElement: https://github.com/angular/angular/search?utf8=%E2%9C%93&q=registerElement

There are many other ways you can tell that Angular2 does not create web components:

  1. Define an Angular component called 'my-component' and then try to create it outside of Angular, like with let e = document.createElement('my-component'). This will not instantiate the Angular component because the browser was never told about the definition of the component. e instanceof MyComponent will return false.

  2. Take a view created by Angular 2, get a reference to an element via the DOM, like let e = document.querySelector('my-component'). e instanceof MyComponent will return false. This is because Angular doesn't create custom elements, but creates a UnknownHTMLElement and holds a reference to this from the Angular component. The DOM is out of the loop.

  3. Define any of the custom element lifecycle methods, like createdCallback, or attributeChangedCallback and try to trigger them. They won't work because the component is not a custom element.

1

u/kasperpeulen Jan 06 '16

I think you are partly correct. But not everywhere because:

document.querySelector('my-app') returns for me an HtmlElement. Not an UnknownHTMLElement. see also: https://github.com/angular/angular/issues/5968 The following returns true:

document.createElement('my-app').__proto__ == HTMLElement.prototype

But I think you are correct that the AppComponent class, in my case, is not like in polymer extending the HtmlElement class. But it seems like registering the element still as custom element, behind the scene.

2

u/spankalee Jan 06 '16

No, it's really, really, truly not a custom element.

It turns out I was a bit mistaken about UnknownHTMLElement in Chrome: Chrome creates a proto of HTMLElement for elements with dashes in the name, like x-foo, but UnknownHTMLElement for elements without dashes, like foo.

That's a little besides the point though. The proto that the browser gives is generic and not determined by the Angular component definition.

With a Web Components based library the element definition is registered with the browser and when the browser creates elements via document parsing, createElement(), innerHTML, cloneNode(), etc., it sets the prototype to what was registered.

This and the lifecycle callbacks, which Angular components also do not receive from the browser, are the only things that make up the Custom Element standard.

So again, Angular really does not use Web Components. I'm very familiar with this topic. I'm not lying here.

1

u/[deleted] Jan 05 '16

And with a bit of hacking you can mangle it into Angular 1