Angular Material - 工具栏小部件

md-toolbar 是一个 Angular 指令,用于显示工具栏,该工具栏通常是内容上方的区域,用于显示标题和相关按钮。


属性

下表列出了md-toolbar的参数及不同属性的说明。

Sr.No 参数和描述
1

md-scroll-shrink

这决定了标题是否应该在用户向下滚动时缩小,并在用户向上滚动时显示出来。

  • 要使 scrollShrink 工作,工具栏必须是 md-content 元素的同级元素,放置在它之前。

  • md-scroll-shrink 属性仅在组件初始化时进行解析,它不会监视范围更改。

2

md-shrink-speed-factor

工具栏收缩速度的改变量。 例如,如果给定 0.25,则工具栏将以用户向下滚动的速率的四分之一缩小。 默认值为 0.5。


示例

以下示例展示了工具栏的使用。

am_toolbar.html

<html lang = "en">
   <head>
      <link rel = "stylesheet"
         href = "https://www.w3ccoo.com/lib/angular_material/1.0.0/angular-material.min.css">
      <script src = "https://cdn.staticfile.org/angular.js/1.4.8/angular.min.js"></script>
      <script src = "https://cdn.staticfile.org/angular.js/1.4.8/angular-animate.min.js"></script>
      <script src = "https://cdn.staticfile.org/angular.js/1.4.8/angular-aria.min.js"></script>
      <script src = "https://cdn.staticfile.org/angular.js/1.4.8/angular-messages.min.js"></script>
      <script src = "https://www.w3ccoo.com/lib/angular_material/1.0.0/angular-material.min.js"></script>
      <link rel = "stylesheet" href = "https://fonts.googleapis.com/icon?family=Material+Icons">
      
      <script language = "javascript">
         angular
            .module('firstApplication', ['ngMaterial'])
            .controller('toolbarController', toolbarController);
           
         function toolbarController ($scope) { 
            var self = this;            
            self.allContacts = loadContacts();
            self.contacts = [self.allContacts[0]];
                        
            function loadContacts() {
               var contacts = [
                  'Roberto Karlos',
                  'Bob Crestor',
                  'Nigel Rick',
                  'Narayana Garner'                  
               ];
               
               return contacts.map(function (c, index) {
                  var cParts = c.split(' ');
                  
                  var contact = {
                     name: c,
                     email: cParts[0][0].toLowerCase() + '.' + cParts[1].toLowerCase()
                        + '@example.com',
                     image: 'http://lorempixel.com/50/50/people?' + index
                  };
                  
                  contact._lowername = contact.name.toLowerCase();
                  return contact;
               });
            } 
         }	  
      </script>      
   </head>
   
   <body ng-app = "firstApplication"> 
      <div id = "toolbarContainer" ng-controller = "toolbarController as ctrl"
         layout = "column" ng-cloak>
         <md-content>
            <md-toolbar md-scroll-shrink>
               <div class = "md-toolbar-tools">
                  <md-button class = "md-icon-button" aria-label = "Settings">
                     <md-icon class = "material-icons">menu</md-icon>
                  </md-button>
                  
                  <h2>
                     <span>Contacts</span>
                  </h2>
                  
                  <span flex></span>
                  <md-button class = "md-icon-button" aria-label = "More">
                     <md-icon class = "material-icons">more_vert</md-icon>
                  </md-button>
               </div>
            </md-toolbar>
            
            <md-list>
               <md-subheader class = "md-no-sticky">Contacts</md-subheader>
               <md-list-item class = "md-2-line contact-item"
                  ng-repeat = "(index, contact) in ctrl.allContacts"
                  ng-if = "ctrl.contacts.indexOf(contact) < 0">
                  <img ng-src = "{{contact.image}}" class = "md-avatar"
                     alt = "{{contact.name}}" />
                  
                  <div class = "md-list-item-text compact">
                     <h3>{{contact.name}}</h3>
                     <p>{{contact.email}}</p>
                  </div>
                  
                  <md-divider ng-if = "!$last"></md-divider>
               </md-list-item>
            </md-list>
            
            <md-list>
               <md-subheader class = "md-no-sticky">Contacts (With Insets)</md-subheader>
               <md-list-item class = "md-2-line contact-item"
                  ng-repeat = "(index, contact) in ctrl.allContacts"
                  ng-if = "ctrl.contacts.indexOf(contact) < 0">
                  <img ng-src = "{{contact.image}}" class = "md-avatar"
                     alt = "{{contact.name}}" />
                  <div class = "md-list-item-text compact">
                     <h3>{{contact.name}}</h3>
                     <p>{{contact.email}}</p>
                  </div>
                  <md-divider md-inset ng-if = "!$last"></md-divider>
               </md-list-item>
            </md-list>
            
         </md-content>	 
      </div>
   </body>
</html>

结果

验证结果。

❮ angular_material_widgets.html