【问题标题】:How to create a boostrap nav-bar and reuse it on every page如何创建引导导航栏并在每个页面上重用它
【发布时间】:2015-10-28 19:41:30
【问题描述】:

我需要在所有页面上重用boostrapnavbar,所以我一直在尝试为菜单创建一个文件并使用ng-includeangularJS标签呈现它

菜单就是这个文件/partials/top_menu.html

<div class="container">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
        </button>
        <a class="navbar-brand" href="index.html">EXAMPLE</a>
    </div>
    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
        <ul class="nav navbar-nav navbar-right">
            <li>
                <a href="about.html">Quienes Somos</a>
            </li>
            <li>
                <a href="organizacion.html">Organizaci&oacute;n</a>
            </li>
            <li>
                <a href="servicios.html">Servicios</a>
            </li>
            <li>
                <a href="clientes.html">Clientes</a>
            </li>
            <li>
                <a href="portfolio-3-col.html">Galeria</a>
            </li>
        </ul>
    </div>
    <!-- /.navbar-collapse -->
</div>

这里是 index.html,它试图包含该部分作为页面的导航菜单

<!DOCTYPE html>
<html lang="en">

<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>TITLE</title>

    <!-- Bootstrap Core CSS -->
    <link href="css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom CSS -->
    <link href="css/modern-business.css" rel="stylesheet">

    <!-- Custom Fonts -->
    <link href="font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
</head>

<body>

    <!-- Navigation -->
    <nav class="navbar navbar-default navbar-fixed-top topnav" role="navigation">
        <div ng-include="'partials/top_menu.html'"></div>
    </nav>
<body>

问题是菜单栏根本没有被渲染,WebDeveloper 控制台没有显示任何错误或文件丢失

我做错了什么,或者如果这是错误的方法,请告诉我我可以实现它

【问题讨论】:

  • 您也可以将
  • 你也可以自定义指令来实现它,但最好使用 ng include
  • @Tomislav 您可能已经看到我的 index.html 指向其他需要包含该菜单的 html 文件。
  • 好吧,我认为您想使用部分和路由来开发 SPA(单页应用程序)。如果不是,那么您的方法是可以的。
  • 您的 index.html 中也缺少 ng-app 指令。试试

标签: css angularjs twitter-bootstrap


【解决方案1】:

使用这个脚本可能对你有帮助:

<html> 
  <head> 
    <script src="jquery.js"></script> 
    <script> 
    $(function(){
      $("#includedContent").load("b.html"); 
    });
    </script> 
  </head> 

  <body> 
     <div id="includedContent"></div>
  </body> 
</html>

必须包含 Jquery 文件。

【讨论】:

    【解决方案2】:

    您需要将ng-app 添加到您的 index.html 的 body 标签中

    <body ng-app>
    

    这是 AngularJS 引导指令。没有这个,AngularJS 进程就不会启动。

    你需要为任何需要使用angularJS指令的html文件这样做

    【讨论】:

    • 这意味着您没有使用 SPA 方法。在 about.html 中,您必须像在 index.html 中一样包含所有头脚本,包含所有 css 文件并设置 .
    • 你真的很想考虑使用路由和局部视图。这就是 AngularJS 的核心
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签