Contact Chips

优质
小牛编辑
137浏览
2023-12-01

md-contact-chips是一个Angular Directive,是一个基于md-chips构建的输入控件,使用md-autocomplete元素。 联系芯片组件接受查询表达式,该表达式返回可能的联系人列表。 用户可以选择其中之一并将其添加到可用芯片列表中。

属性 (Attributes)

下表列出了md-contact-chips的不同属性的参数和描述。

Sr.No参数和描述
1

* ng-model

用于将项列表绑定到的模型。

2

* md-contacts

期望返回与搜索测试$ query匹配的联系人的表达式。

3

* md-contact-name

表示联系人姓名的联系对象的字段名称。

4

* md-contact-email

表示联系人电子邮件地址的联系对象的字段名称。

5

* md-contact-image

表示联系人图像的联系对象的字段名称。

6

placeholder

将转发到输入的占位符文本。

7

secondary-placeholder

将转发到输入的占位符文本,当列表中至少有项目时显示。

8

filter-selected

是否从自动填充中显示的建议列表中筛选选定的联系人。

例子 (Example)

以下示例显示了md-contact-chips指令的使用以及角接触芯片的使用。

am_contact_chips.htm

<html lang = "en">
   <head>
      <link rel = "stylesheet"
         href = "https://ajax.googleapis.com/ajax/libs/angular_material/1.0.0/angular-material.min.css">
      <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
      <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-animate.min.js"></script>
      <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-aria.min.js"></script>
      <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-messages.min.js"></script>
      <script src = "https://ajax.googleapis.com/ajax/libs/angular_material/1.0.0/angular-material.min.js"></script>
      <script language = "javascript">
         angular
            .module('firstApplication', ['ngMaterial'])
            .controller('contactsChipController', contactsChipController);
         function contactsChipController ($scope) {
            var self = this;
            self.querySearch = querySearch;
            self.allContacts = loadContacts();
            self.contacts = [self.allContacts[0]];
            self.filterSelected = true;
            function querySearch (query) {
               var results = query ?
               self.allContacts.filter(createFilterFor(query)) : [];
               return results;
            }
            function createFilterFor(query) {
               var lowercaseQuery = angular.lowercase(query);
               return function filterFn(contact) {
                  return (contact._lowername.indexOf(lowercaseQuery) != -1);;
               };
            }
            function loadContacts() {
               var contacts = [
                  'Roberto Karlos',
                  'Bob Crestor',
                  'Nigel Rick',
                  'Narayana Garner',
                  'Anita Gros',
                  'Megan Smith',
                  'Tsvetko Metzger',
                  'Hector Simek',
                  'James Roody'
               ];
               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 ng-controller = "contactsChipController as ctrl" layout = "column"
         ng-cloak>
         <md-content class = "md-padding autocomplete" layout = "column">
            <md-contact-chips
               ng-model = "ctrl.contacts"
               md-contacts = "ctrl.querySearch($query)"
               md-contact-name = "name"
               md-contact-image = "image"
               md-contact-email = "email"
               md-require-match = "true"
               md-highlight-flags = "i"
               filter-selected = "ctrl.filterSelected"
               placeholder = "To">
            </md-contact-chips>
            <md-list class = "fixedRows">
               <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-list-item>
               <md-list-item class = "md-2-line contact-item selected"
                  ng-repeat = "(index, contact) in ctrl.contacts">
                  <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-list-item>
            </md-list>
         </md-content>
      </div>
   </body>
</html>