3

i'm trying to teach myself angular2. I trying to build component "trigger-resize", which i want to render as :

<a class="hidden-xs" href="">
    <em class="fa fa-navicon"></em>
</a>

and NOT as:

<trigger-resize>

   <a class="hidden-xs" href="">
      <em class="fa fa-navicon"></em>
   </a>
</trigger-resize>

(i dont want custom selector to render)

In angular 1. i know it would be achieved by "replace:true" option, but is it possible to achieve it in angular2?

Kind Regards

happyZZR1400
  • 2,387
  • 3
  • 25
  • 43

3 Answers3

5

One way to do it is to use an attribute

<a triggerResize class="hidden-xs" href=""></a>

Which has a component like

@Component({
    selector : 'a[triggerResize]', //select all <a> tags with triggerResize attribute
    template : '<em class="fa fa-navicon"></em>'
})

CamelCase attributes are the proper syntax now for custom attributes in angular2

Poul Kruijt
  • 69,713
  • 12
  • 145
  • 149
1

You can use an attribute on <a> as selector instead of a tag like <a trigger-resize class="hidden-xs"... > and then in the component annotation use [trigger-resize] as selector.

<a trigger-resize class="hidden-xs" href="">
    <em class="fa fa-navicon"></em>
</a>
@Component({
    selector : '[triggerResize]'
    template : '<em class="fa fa-navicon"></em>'
})

This is also quite useable for other situations where specific tag names are required like <li> inside <ul> or <tr> inside `

<ul>
  <li myLi></li>
</ul>

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
1

The direct answer to the question is "no" – your custom element/selector will be in the HTML. To quote the Angular 1 to Angular 2 Upgrade Strategy doc:

Directives that replace their host element (replace: true directives in Angular 1) are not supported in Angular 2. In many cases these directives can be upgraded over to regular component directives.

That said, for your specific use case, as others have already mentioned, a component that uses an attribute selector would work.

See also

Community
  • 1
  • 1
Mark Rajcok
  • 362,217
  • 114
  • 495
  • 492