0

I am very new to AngularJs and whilst I know javascript well I'm still getting to grips with the way things works with Angular.

I am trying to generate a site navigation and can generate the top level links fine. However I am stuck with how to generate the subnav. This is what I would like to do;

<ul id='topNav'>
    <li ng-repeat="item in nav"><a href="{{item.url}}" title="{{item.title}}">{{item.title}}</a>
        <ul class="subNav" if(item.subNav.length > 0)>
            <li ng-repeat="subItem in subNav"><a href="{{subItem.url}}" title="{{subItem.title}}">{{subItem.title}}</a></li>
        </ul>
    </li>
</ul>

So, where the if(item.subNav.length > 0) appears I would like to replace that with the Angular equivalent. item.subNav is an array which is either empty or populated with the links for the subnav. If it's empty then no html is generated, otherwise loop through the links. I don't want any empty <ul class="subNav"></ul> tags.

Any help is greatly appreciated.

1

1 Answer 1

3

The directive is named ngIf:

<ul class="subNav" ng-if="item.subNav.length > 0">

or even simpler, since 0 is falsy:

<ul class="subNav" ng-if="item.subNav.length">
Sign up to request clarification or add additional context in comments.

Comments

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.