Angular Material - Contact Chips 小部件
md-contact-chips 是一个 Angular 指令,是一个基于 md-chips 构建的输入控件,并使用 md-autocomplete 元素。contact-chips 组件接受查询表达式,该表达式返回可能的联系人列表。 用户可以选择其中之一并将其添加到可用 chips 列表中。
属性
下表列出了md-contact-chips的参数及不同属性的说明。
Sr.No | 参数和描述 |
---|---|
1 | * ng-model 将项目列表绑定到的模型。 |
2 | * md-contacts 预期返回与搜索测试 $query 匹配的联系人的表达式。 |
3 | * md-contact-name 代表联系人姓名的 contact 对象的字段名称。 |
4 | * md-contact-email 表示联系人电子邮件地址的联系人对象的字段名称。 |
5 | * md-contact-image 表示联系人图像的联系人对象的字段名称。 |
6 | placeholder 将转发到输入的占位符文本。 |
7 | secondary-placeholder 将转发到输入的占位符文本,当列表中至少有一个项目时显示。 |
8 | filter-selected 是否从自动完成显示的建议列表中过滤选定的联系人。 |
示例
以下示例展示了 md-contact-chips 指令的使用以及 angular contact chips 的使用。
am_contact_chips.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> <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>
结果
验证结果。
❮ angular_material_widgets.html