13

I am looking for a way to have a component that is rendered only with its content. For example, Given the component:

@Component({selector: 'my-cmp', template: '<div> my-cmp </div>'})
class MyComponent {
}

rendering it with angular2 will add the following to the DOM:

<my-cmp>
    <div> my-cmp </div>
</my-cmp>

While I want to find a way to render it directly as:

<div> my-cmp </div>

Sadly since angular 2 is relatively new (just went beta) - Google is not so helpful, not to mention how lacking the current documentation is..

Roham Rafii
  • 2,929
  • 7
  • 35
  • 49
bennyl
  • 2,886
  • 2
  • 29
  • 43

4 Answers4

15

Sort of the same can be achieved by using an attribute selector

camelCasing is mandatory for attributes since alpha.52

@Component({
     selector: '[myCmp]', //attribute selector
     template: 'my-cmp'
})
class MyComponent {
}

And use this to call it:

<div myCmp></div>
Poul Kruijt
  • 69,713
  • 12
  • 145
  • 149
3

You can do somethink like the following:

@Component({selector: 'div.my-cmp', template: 'my-cmp'})
class MyComponent {
}

and in your HTML:

 <div class="my-cmp" />
Shai Aharoni
  • 1,955
  • 13
  • 25
2

According to documentation of angular 1.x

Replace the directive's element itself (if replace is true - DEPRECATED).

So i think this feature won't be available it angular 2

Denis
  • 747
  • 4
  • 13
0

I remember asking a similar question for Angularjs a few years ago. The concensus seemed to be then, as it is might be now is you probably don't want to do that.

It breaks semantics: a <video/> or a <input/> does not decompose into its contents. Your Angular components should not either.

You can probably still achieve what you want by thinking more deeply about your markup/css? Perhaps if you flesh out your use case we might be able to give you an alternative?

Ashley Coolman
  • 11,095
  • 5
  • 59
  • 81