admin管理员组文章数量:1313347
i'm developing a mobile based on Angularjs and Angular-Material but i'm a bit confused on how to set up a menu.
here's my semplified situation:
inside of my body
i have my md-sidenav
and an md-content
, inside of my md-sidenav
there are all the md-menu-buttons
.
Question: how can i implement those md-menu-buttons
so that clicking them, a different content is shown in md-content
.
For example when i click, 'home' a div
with all the home's stuff appears and so on?
i'm developing a mobile based on Angularjs and Angular-Material but i'm a bit confused on how to set up a menu.
here's my semplified situation:
inside of my body
i have my md-sidenav
and an md-content
, inside of my md-sidenav
there are all the md-menu-buttons
.
Question: how can i implement those md-menu-buttons
so that clicking them, a different content is shown in md-content
.
For example when i click, 'home' a div
with all the home's stuff appears and so on?
2 Answers
Reset to default 4You probably use AngularJS with SPA (single page application) concept in mind, so why not make a try with an AngularJS routing module - which are particularly built for such purposes.
There are two popular routing modules for AngularJS, namely:
- ngRoute
- UI router
For the sake of simplicity, you can go with the first one.
Here's how you can set up your routes to display different templates into the ngView
directive whenever you navigate to a particular route:
HTML
<!-- Your md-buttons -->
<a md-button href="#dashboard">Dashboard</a>
<a md-button href="#some-other-route">Some other route</a>
<!-- ... -->
<md-content>
<div ng-view></div> <!-- This is where your templates (and their controllers) are injected -->
</md-content>
JS
angular.module('yourApp', [
'ngRoute',
// other dependencies
])
.config(function($routeProvider){
/*
* set up your routes here
*/
$routeProvider.
when('/dashboard', {
template: '...' // alernatively, `templateUrl` if your partial stays somewhere else,
controller: function (){
/* custom logic for the template */
}
})
.when('/some-other-route', {
template: '...',
controller: function(){ ... }
});
})
Plunker Demo
Note: Do not forget to open the side-nav from the top left if it's not open already in the Plunker demo.
Note
Do not forget to load the script for ngRoute
somewhere after loading angularJS in your HTML.
You may also take a look at $locationProvider
service's html5Mode
method for configuring how routes are to work (e.g. /dashboard
instead of #dashboard
) in your links.
Here's a simple example - CodePen
Markup
<div ng-controller="AppCtrl" layout="column" style="height:500px;" ng-cloak="" class="sidenavdemoBasicUsage" ng-app="MyApp">
<section layout="row" flex="">
<md-sidenav class="md-sidenav-left" md-ponent-id="left" md-whiteframe="4" id="leftSideNav" md-disable-backdrop="true">
<md-toolbar class="md-theme-indigo" layout="row">
<h1 class="md-toolbar-tools">Sidenav Left</h1>
<span flex></span>
<md-button ng-click="close()">Close</md-button>
</md-toolbar>
<md-content layout-padding="" layout="column" layout-align="start start">
<md-button ng-click="show('home')" class="md-primary">Show Home</md-button>
<md-button ng-click="show('work')" class="md-primary">Show Work</md-button>
</md-content>
</md-sidenav>
<md-content flex="" layout-padding="" layout="column" layout-align="top center">
<md-button ng-click="toggleLeft()" class="md-primary">
Toggle left
</md-button>
<div ng-switch="toShow">
<div ng-switch-when="home">
Home!
</div>
<div ng-switch-when="work">
Work!
</div>
</div>
</md-content>
</section>
</div>
JS
angular
.module('MyApp',['ngMaterial', 'ngMessages', 'material.svgAssetsCache'])
.controller('AppCtrl', function ($scope, $mdSidenav) {
$scope.toShow = "home"; // Default
$scope.toggleLeft = function() {
$mdSidenav("left")
.toggle();
};
$scope.close = function () {
$mdSidenav('left').close();
};
$scope.show = function (toShow) {
$scope.toShow = toShow;
};
});
本文标签: javascriptAngularmaterial how to build a sidenav menu that control a mdcontentStack Overflow
版权声明:本文标题:javascript - Angular-material how to build a sidenav menu that control a md-content? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741935602a2405837.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论