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?

Share Improve this question asked Sep 2, 2016 at 7:48 Davide NaleDavide Nale 1791 gold badge5 silver badges20 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

You 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