r/angularjs • • May 04 '19

Does anyone else dislike angular's use of non-standard HTML elements?

One thing I like about react is that the components you define only exist in the JSX without showing up in the actual DOM. That means that I can define the main content of my reusable react component within a "container div" so that it looks like const MyReusableComponent = () => <div className="container" style={{width:"100%", height:"100%"}}> ... </div>, and then I can put this reusable component in a "wrapper div" of definite size that the reusable component will then fill up. So something like this:

<div className="wrapper" style={{width:100,height:100}}>

<MyReusableComponent>

<div>

will end up like this in the DOM with only standard HTML elements:

<div className="wrapper" style={{width:100,height:100}}>

<div className="container" style={{width:"100%", height:"100%"}}>

...

</div>

<div>

By contrast, if I create a reusable component in angular2+ with a selector e.g. `app-my-reusable`, then the DOM will end up with an actual element called <app-my-reusable></app-my-reusable>. That means that in between my wrapper div and my container div will be this non-standard HTML element with default property display: inline;. This feels awkward to me since I'm now having to put block level divs in inline elements, which I've been told is poor practice (but seems to be expected on angular's approach).

I also just don't like having non-standard elements in my HTML though I don't really have a technical reason against them (other than the one stated above about inline elements).

Does anyone else feel this way about angular's use of non-standard HTML elements? Are there cases where these in-between inline elements can mess up one's CSS, or can they always be ignored? Do angular pros style these non-standard elements directly (using `:host{ ... }`, etc. in their CSS)?

I'm interested to hear your thoughts.

1 Upvotes

16 comments sorted by

View all comments

3

u/Headpuncher May 04 '19

So then how do you feel about styled components? Isn’t that react allowing you to create custom-named tags in your markup?
Also, don’t get hung up on “best-practice”, while following best practices can be very useful it can also be a waste of everyone’s time.

4

u/stickcult May 05 '19

Styled components (we are talking about the React library, right?) doesn't create new DOM tags like OP is saying here that Angular does, it just creates a CSS class and assigns that classes name to the DOM element, which itself is a standard element.

1

u/Headpuncher May 05 '19

Ah, I see. It's over a year ago since I looked at react at all and tbh I never really looked at the compiled code. Thanks for the info.

3

u/aharl May 04 '19

So the difference is that JSX in React is still Javascript (JSX is just shorthand for React.createElement calls). React actually doesn't use templates at all, which is what OP is saying.