我已经发现Github 上的 Mailcheck.js这正是我想要的,通过提供诸如“你的意思是[电子邮件受保护]?"
但是,该库适用于纯 Javascript / jQuery。我需要 AngularJs 包装器,所以我稍微修改了一下角度邮件检查这是结果指令:
(function () {
'use strict';
/**
* @ngdoc directive
* @name mailcheck.directive:mailcheck
* @description
* Angular wrapper for Mailcheck.js
*/
function mailcheckDirective($compile, $sce) {
return {
restrict: 'A',
replace: false,
link: function (scope, el, attrs) {
//Mailcheck.defaultDomains.push('yandex.ru', 'rambler.ru', 'bk.ru', 'ukr.net', 'list.ru', 'inbox.ru', 'yandex.ua', 'ya.ru', 'i.ua', 'inbox.lv', 'mail.ua', 'yandex.com', 'abv.bg', 'icloud.com', 'meta.ua', 'tut.by', 'rediffmail.com');
Mailcheck.defaultTopLevelDomains.push('com.id', 'com.ph', 'com.br', 'com.vn', 'com.in');
// Limit to input element of specific types
var inputTypes = /text|email/i;
if (el[0].nodeName !== 'INPUT') {
throw new Error('angular-mailcheck is limited to input elements');
}
if (!inputTypes.test(attrs.type)) {
throw new Error('Invalid input type for angular-mailcheck: ' + attrs.type);
}
scope.suggestion = false;
scope.bugmenot = false;
// Compiled template
if (attrs.mailcheck !== "notemplate") {
var template = $compile('<div class="help-block mailcheck" ng-show="suggestion && !bugmenot">Did you mean <a ng-bind="suggestion" ng-click="useSuggestion()"></a>? <a ng-click="suggestion=false;bugmenot=true">Nope.</a></div>')(scope);
el.after(template);
}
el.bind('input', function () {
scope.suggestion = false;
})
.bind('blur', function () {
el.mailcheck({
suggested: function (element, suggestion) {
scope.suggestion = suggestion.full;
scope.$apply();
},
empty: function (element) {
scope.suggestion = false;
}
});
});
scope.useSuggestion = function () {
el.val(scope.suggestion);
scope.suggestion = false;
};
}
};
}
angular
.module('angular-mailcheck', [])
.directive('mailcheck', mailcheckDirective);
mailcheckDirective.$inject = ['$compile', '$sce'];
})();
一旦指令成为解决方案的一部分,就可以在 HTML 中像这样使用:
<input mailcheck="notemplate" />
<small class="mailcheck" ng-show="suggestion && !bugmenot">
<span>Did you mean</span> <a ng-bind="suggestion" ng-click="useSuggestion()"></a>?
<a ng-click="suggestion=false;bugmenot=true">Nope</a>.
</small>
如果您不需要在 HTML 中自定义邮件检查块,您可以使用mailcheck=""
属性而不是mailcheck="notemplate"
.