Angular ui.router默认页眉页脚

Angular ui.router default header footer

本文关键字:默认 ui router Angular      更新时间:2023-09-26

当谈到ui.router模块时,我可以想出三种不同的方法来为每个视图设置默认的页眉和页脚:

DEFAULT HEADER
<CONTENT>
DEFAULT FOOTER

1.ng-include-将页眉/页脚附加到初始.html文件(index.html)中

<html>
<div ng-include src="'header.html'"></div>
<div id="content" ui-view></div>

1.1.将代码粘贴到index.html

<html>
<div><!-- my header code here --></div>
<div id="content" ui-view></div>

2.使用指令解析页眉和页脚

home.html

<!-- content -->
<!-- /content -->
<footer></footer>

footerDirective.js

module.directive('footer', function () {
    return {
        restrict: 'E',
        replace: true,
        templateUrl: "footer.html",
        controller: ['$scope', '$filter', function ($scope, $filter) {
        }]
    } 
});

http://gon.to/2013/03/23/the-right-way-of-coding-angularjs-how-to-organize-a-regular-webapp/

3.在没有url的ui.router上创建一个额外的状态

然后,状态包装器将包含页眉和页脚,并且不可调用。

$stateProvider
.state('wrapper', {
    templateUrl: 'wrapper.html', // contains html of header and footer
    controller: 'WrapperCtrl'
})
.state('wrapper.home', {
    url: '/',
    templateUrl: 'home.html',
    controller: 'HomeCtrl'
});

哪一个是首选?或者,对于Angular 1.x,有没有更可取的方法呢?

还有另一种方法可以利用状态的views属性。它允许为某个状态定义多个命名视图。UI文档。

考虑以下示例,其中状态myapp有三个命名视图,其中内容视图将是具有动态内容的视图。

$stateProvider
    .state('myapp', {
        views: {
          'header': {
            template:'header <hr />',
            controller:'mainController as main'
          },
          'content': {
            template:'<div ui-view></div>'
          },
          'footer': {
            template:'<hr /> footer',
            controller:'mainController as main'
          }
       }
   })
  //States below will live in content view
  .state('myapp.one', {
    template:'View 1 <button ui-sref="myapp.two">next page</button>',
    controller:'firstController as first',
  })
  .state('myapp.two', {
    template:'Another page <button ui-sref="myapp.one"> Go back</button>',
    controller:'secondController as second',
  })

HTML将如下所示:

<div ui-view="header"></div>
<div ui-view="content"><!-- Where your content will live --></div>
<div ui-view="footer"></div>

Jsbin示例