Angular ui.router默认页眉

时间:2021-08-24 10:08:24

When it comes to ui.router modules I can think of three different ways to set a default header and footer for every view:

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

DEFAULT HEADER
<CONTENT>
DEFAULT FOOTER

1. ng-include - attaching your header / footer into your initial .html file (index.html).

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

1.1. Pasting code into index.html

1.1。将代码粘贴到index.html中

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

2. Using directives to parse the header and footer.

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. Creating an extra state on ui.router with no url.

State wrapper would then contain the header and footer and won't be callable.

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

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

Which one is preferred? Or, is there a more desirable way to do it with Angular 1.x?

哪一个更受欢迎?或者,使用Angular 1.x有更好的方法吗?

1 个解决方案

#1


11  

There is also another way where you take advantage of the state's views property. It enables one to define multiple named views for a certain state. UI docs.

您还可以通过另一种方式利用州的景观资产。它使人们能够为某个状态定义多个命名视图。 UI文档。

Consider the below example where state myapp has three named views, where the content view will be the view with dynamic content.

请考虑以下示例,其中状态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',
  })

And the HTML will look like this:

HTML将如下所示:

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

Jsbin example

#1


11  

There is also another way where you take advantage of the state's views property. It enables one to define multiple named views for a certain state. UI docs.

您还可以通过另一种方式利用州的景观资产。它使人们能够为某个状态定义多个命名视图。 UI文档。

Consider the below example where state myapp has three named views, where the content view will be the view with dynamic content.

请考虑以下示例,其中状态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',
  })

And the HTML will look like this:

HTML将如下所示:

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

Jsbin example