Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replace component with its content - angular 2 [duplicate]

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..

like image 824
bennyl Avatar asked Dec 24 '15 14:12

bennyl


3 Answers

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>
like image 91
Poul Kruijt Avatar answered Nov 16 '22 12:11

Poul Kruijt


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" />
like image 35
Shai Aharoni Avatar answered Nov 16 '22 12:11

Shai Aharoni


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

like image 2
Denis Avatar answered Nov 16 '22 13:11

Denis