admin管理员组文章数量:1287596
I am fairly new at Angular and trying to build a static website using angular and bootstrap. This is what I have done so far to create navigation (and footer using same method) to add to every pages in the website. Is there any better(more efficient) way building templates with Angular? Is this what everyone does for these type of applications?
app.directive("mynav", function () {
return {
restrict: "A",
template: "<nav class='navbar navbar-default' role='navigation'><div class='navbar-inner'><div class='container-fluid'><div class='navbar-header'><button type='button' class='navbar-toggle collapsed' data-toggle='collapse' data-target='#menu-nav'><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='#'><img alt='img' src='img/logo.svg' height='30'></a></div><div class='collapse navbar-collapse' id='menu-nav'><ul class='nav navbar-nav navbar-right'><li><a href='pages/home.html'>Home</a></li><li><a href='pages/about.html'>About</a></li><li><a href='pages/service.html'>Services</a></li><li><a href='pages/product.html'>Rental Products</a></li><li><a href='pages/contact.html'>Contact</a></li><button type='button' class='btn btn-primary navbar-btn'>Request A Quote</button></ul></div></div></div></nav>"
}
});
<header mynav></header>
I am fairly new at Angular and trying to build a static website using angular and bootstrap. This is what I have done so far to create navigation (and footer using same method) to add to every pages in the website. Is there any better(more efficient) way building templates with Angular? Is this what everyone does for these type of applications?
app.directive("mynav", function () {
return {
restrict: "A",
template: "<nav class='navbar navbar-default' role='navigation'><div class='navbar-inner'><div class='container-fluid'><div class='navbar-header'><button type='button' class='navbar-toggle collapsed' data-toggle='collapse' data-target='#menu-nav'><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='#'><img alt='img' src='img/logo.svg' height='30'></a></div><div class='collapse navbar-collapse' id='menu-nav'><ul class='nav navbar-nav navbar-right'><li><a href='pages/home.html'>Home</a></li><li><a href='pages/about.html'>About</a></li><li><a href='pages/service.html'>Services</a></li><li><a href='pages/product.html'>Rental Products</a></li><li><a href='pages/contact.html'>Contact</a></li><button type='button' class='btn btn-primary navbar-btn'>Request A Quote</button></ul></div></div></div></nav>"
}
});
<header mynav></header>
Share
Improve this question
asked Dec 17, 2014 at 11:48
Saeed AbbaspourSaeed Abbaspour
3294 silver badges16 bronze badges
2
- Is your navigation and footer fixed for all the pages? – Aniket Sinha Commented Dec 17, 2014 at 11:58
- @AniketSinha yet it is. – Saeed Abbaspour Commented Dec 17, 2014 at 12:32
3 Answers
Reset to default 4In case your navbar is also static like footer, you can exclude it from ng-view
Your html structure can be something like :
<html>
<head>...</head>
<body>
<div mynav></div>
<div ng-view></div>
<div my-footer></div>
</body>
</html>
In this case, the navigation bar will be static too just like your footer. So all your views will be loaded at <div ng-view></div>
and mynav
and my-footer
div will be untouched.
Also,In your directives, you can replace inline template
with templateUrl
. Create a new HTML, say my-nav.html
and use templateUrl: "path/to/my-nav.html"
,
You dont need a directive for static html.
Usually you have a shell page (main page), in which you put all the parts that should be on all (or most) pages. And then you load you views into that page using angular's ng-view (or ui-view if you use ui-router for routing).
Something like this:
<html ng-app='app'>
<head>...</head>
<body>
<div ng-controller="navbarCtrl as vm">
<div ng-include="'navbar.html'"></div>
</div>
<!-- Your main view loads here, along with its controller
as defined in your routing (check ngRoute or ui-router for more)-->
<div ng-view></div>
<footer>Your footer es here (or can be ng-included)</footer>
</body>
</html>
Take a look at custom directives at https://docs.angularjs/guide/directive.
And your directive will be like this:
app.directive('name', function() {
restrict: 'E',
templateUrl: 'page-title'.html
}
This is a better way, not only more tidy, it's also rendering faster. I learn this at free course on schoolcode that pointed out from angular website.
Note: by rendering time I mean ng-include
will be loaded after javascript and html are all loded.
本文标签: javascriptAdding navigation and footer to every page using AngularJSStack Overflow
版权声明:本文标题:javascript - Adding navigation and footer to every page using AngularJS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741273666a2369604.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论