Angular Material - Contact Chips



The md-contact-chips, an Angular Directive, is an input control built on md-chips and uses the md-autocomplete element. The contact chip component accepts a query expression which returns a list of possible contacts. The user can select one of these and add it to the list of availble chips.

Attributes

The following table lists out the parameters and description of the different attributes of md-contact-chips.

Sr.No Parameter & Description
1

* ng-model

A model to bind the list of items to.

2

* md-contacts

An expression expected to return contacts matching the search test, $query.

3

* md-contact-name

The field name of the contact object representing the contact's name.

4

* md-contact-email

The field name of the contact object representing the contact's email address.

5

* md-contact-image

The field name of the contact object representing the contact's image.

6

placeholder

Placeholder text that will be forwarded to the input.

7

secondary-placeholder

Placeholder text that will be forwarded to the input, displayed when there is at least on item in the list.

8

filter-selected

Whether to filter selected contacts from the list of suggestions shown in the autocomplete.

Example

The following example shows the use of the md-contact-chips directive and also the use of angular 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>

Result

Verify the result.

angular_material_widgets.htm
Advertisements