AngularJS - Handle repeated fragments like Header and Footer

前端 未结 1 365
长情又很酷
长情又很酷 2020-12-09 23:33

I have been trying to implement the header / footer in an Angular JS App. I was thinking of adding these as ng-include in the main index.html. However this would have worked

相关标签:
1条回答
  • 2020-12-09 23:59

    Use a controller in the header/footer, as ivarni suggested. An example from an (experimental) app of my own:

    In the index.html, the header will display a dynamically generated menu, login/logout etc:

    <div id="navbar" class="navbar navbar-inverse navbar-fixed-top"
        x-ng-controller="NavbarCtrl" x-ng-include="'app/main/navbar.html'"></div>
    

    The NavbarCtrl builds the appropriate scope for the app/main/navbar.html template. The template would be as follows (taking into account your needs - and irrelevant details removed):

    <div class="navbar-inner" x-ng-if="showHeader">
        <div class="container">
            <div>
                <ul class="nav">
                    <li x-ng-repeat="menuEntry in menuEntries">
                        <a x-ng-href="#{{menuEntry.path}}">{{menuEntry.display}}</a>
                    </li>
                </ul>
            </div>
        </div>
        <div x-ng-if="userData.loggedIn">
            Wellcome {{userData.userName}}!
            <a x-ng-click="logout()">Logout</a>
        </div>
        <div x-ng-if="!userData.loggedIn">
            <a x-ng-click="login()">Login</a>
        </div>
    </div>
    

    So the entire markup is hidden depending on the showHeader scope variable. It creates the menu dynamically (menuEntries). And depending on userData.loggedIn, the appropriate Login/Logout message.

    0 讨论(0)
提交回复
热议问题