0

I am making a menu that has a few different types of buttons with AngularJS. Based on the type of button, the html needs to have different characteristics. Also, the currently selected menu item needs to have a different color than the available buttons. Currently, I am using this mess of code:

<div id="navbar" ng-show="navbar.show" ng-mouseenter="navbar.keep()" ng-mouseleave="navbar.release()">

    <div ng-repeat="navSection in navbar.navSections" ng-init="sectionIndex = $index" class="navblock">
        <div ng-repeat="navItem in navSection.navigationItems">
            <div ng-switch on="navItem.function">
                <div ng-switch-when='CategoryNav', ng-click='navbar.navClick(sectionIndex, $index)'>
                    <div info="navItem", ng-if='sectionIndex == navbar.section && $index == navbar.item', id="selected">
                        <a ui-sref="{{navItem.function}}" ng-href="{{navItem.function}}">
                            <div class="navbutton">{{navItem.label}}</div>
                        </a>
                    </div>
                    <div info="navItem", ng-if='sectionIndex != navbar.section || $index != navbar.item', id="notselected">
                        <a ui-sref="{{navItem.function}}" ng-href="{{navItem.function}}">
                            <div class="navbutton">{{navItem.label}}</div>
                        </a>
                    </div>
                </div>

                <div ng-switch-when='StorefrontNav', ng-click='navbar.changeStorefront(navItem.type)'>
                    <div info="navItem", id="notselected">
                        <a ui-sref="{{navItem.function}}" ng-href="{{navItem.function}}">
                            <div class="navbutton">{{navItem.label}}</div>
                        </a>
                    </div>
                </div>

                <div ng-switch-default>
                    <div info="navItem", ng-if='sectionIndex == navbar.section && $index == navbar.item', id="selected">
                        <a ui-sref="{{navItem.function}}" ng-href="{{navItem.function}}">
                            <div class="navbutton">{{navItem.label}}</div>
                        </a>
                    </div>
                    <div info="navItem", ng-if='sectionIndex != navbar.section || $index != navbar.item', id="notselected">
                        <a ui-sref="{{navItem.function}}" ng-href="{{navItem.function}}">
                            <div class="navbutton">{{navItem.label}}</div>
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>

</div>

It works fine but I want to clean it up a little bit. I have thought about making a directive and passing in the functions the different links need to work (for example, the navbar.changeStoreFront()) but that seems like a lot of extra code just to clean the format up a bit. Does anyone know any better ways of cleaning this up?

3
  • A directive or two seems the way to go for me. It's not that much code, if you use the defaults and options that keep it short. Directives will also keep it DRY, so changes will require less work in the future. Commented Jul 2, 2015 at 18:16
  • Hmm. I suppose keeping it DRY is a good enough reason to use directives right there. Good call. Commented Jul 2, 2015 at 18:21
  • The area I had the most trouble when doing this was in getting the Angular expressions correct in the directive template. Also, make sure you know how/when to use '=', '@' to bind to the outer scope, assuming you're using an isolate. Commented Jul 2, 2015 at 18:27

1 Answer 1

1

I noticed that the simplest way to make the code more readable is to put the attributes on separate lines. Take this for example:

<ui-gmap-google-map center='{expression}'
                    control='{Object}'
                    zoom='{expression}'
                    dragging='{expression}'
                    refresh='{expression}'
                    options='{expression}'
                    events='{expression}'
                    bounds='{expression}'
                    pan='{string or boolean}'
        >

    <!-- other ui-gmap-directives here -->

</ui-gmap-google-map>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.