1

I was looking around for a while now and cannot seem to find the right answer... this should be something very trivial... and be documented everywhere... just seems cannot find it...

so, I have a very, very easy component/directive called navItem

This is how I call it

<ul>
    <nav-item route="/aboutus">About us</nav-item>
    <nav-item route="/contactUs">Contact us</nav-item>
</ul>

and this is what is inside it (nav-item.html):

<li><a [routerLink]="route"><ng-content></ng-content></a></li>

so basically I have just this:

import { Component, Input } from '@angular/core';
import { ROUTER_DIRECTIVES } from '@angular/router';

@Component({
    moduleId: module.id,
    selector: 'nav-item',
    template: require('./nav-item.html'),
    directives: [ROUTER_DIRECTIVES]
})
export class NavItem {
    @Input() route = '/';


    constructor(){}
}

What I get is

<ul>
    <nav-item _ngcontent-xng-6="" route="/aboutus" ng-reflect-route="/aboutus"><li><a ng-reflect-router-link="/aboutus" ng-reflect-href="/aboutus" href="/aboutus">About us</a></li></nav-item>
    <nav-item _ngcontent-xng-6="" route="/contactus" ng-reflect-route="/contactus"><li><a ng-reflect-router-link="/aboutus" ng-reflect-href="/contactus" href="/contactus">Contact us</a></li></nav-item>
</ul>

Which of course I don't want.

What I want is replacing <nav-item> </nav-item> entirely with <li><a></a></li>

In AngularJs that was very simple.

1
  • What's in './nav-item.html'? Commented Jul 19, 2016 at 12:07

2 Answers 2

2

This is not supported and I'm pretty sure it won't be in the future. replace: true was deprecated also in Angular1 since quite some time.

What you can do is using attribute selectors like

@Component({
    moduleId: module.id,
    selector: '[nav-item]',
    template: require('./nav-item.html'),
    directives: [ROUTER_DIRECTIVES]
})
export class NavItem {
    @Input() route = '/';


    constructor(){}
}
<a [routerLink]="route"><ng-content></ng-content></a>

and use it like

<ul>
    <li nav-item route="/aboutus">About us</li>
    <li nav-item route="/contactUs">Contact us</li>
</ul>
Sign up to request clarification or add additional context in comments.

7 Comments

Hmm, this is not very component friendly... in optic to have comprehensive markup instead of html, non descriptive markup...so <tabset><tab>Tab1</tab><tab>Tab2</tab></tabset> is not possible anymore you would need to do <ul tabset><li tab>Tab1</li><li tab>Tab 2</li></ul> .... Not really clean... Very, very impressed by A2 so far, but this is quite a big let down :'(
Replacing elements causes just to many issues. I'd rather not use <ul> in the first place, then you can have <nav-item> children or whatever you want.
I would, but bootstrap is styling basing (only) on the elements themselves and not classes... afaik (at least when targetting LIs in nav)
Then the issue is that bootstrap is not flexible enough for Angular2 ;-)
You can add @HostBinding('class.is-active') isActive:boolean = false; to NavItem and when you set this.isActive = true the is-active class gets added to <nav-item class="is-active">
|
0

I think what you want is an Attribute-directive not a component as suggested above.

1 Comment

Can you improve your answer with more information about why?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.