简体   繁体   English

AngularJS输入中@符号后的完整电子邮件域

[英]Complete email domain after @ symbol in AngularJS input

I'm trying to create a function that will automatically detect if a user has typed '@' symbol and will autocomplete the field with corporate domain. 我正在尝试创建一个函数,该函数将自动检测用户是否输入了“ @”符号,并将使用公司域自动填写该字段。 There might be multiple fields on the page that's why I don't want to hardcode theirs models (eg $scope.user.email ). 页面上可能有多个字段,这就是为什么我不想对他们的模型进行硬编码的原因(例如$scope.user.email )。
Here' what I got so far: 这是到目前为止我得到的:

<input ng-model="user.email" ng-keyup="autocompleteEmail($event);">

And corresponding controller code: 以及相应的控制器代码:

$scope.autocompleteEmail = function($event) {
    if (($event.keyCode === 48 || $event.keyCode === 50) && $event.srcElement.value.slice(-1) === "@") {
      // @ symbol is typed - completing email
      $event.srcElement.value += "mycompany.com";
    }
}

Autocompletion works fine but the problem appears as soon as I try to submit the form with this new value - it doesn't take into account mycompany.com domain that I've added automatically. 自动完成功能可以正常工作,但是当我尝试使用此新值提交表单时,该问题就会出现-它不考虑我自动添加的mycompany.com域。 And request that is being sent has user.email = test@ . 并且正在发送的请求具有user.email = test@

How can I achieve this autocomplete functionality with AngularJS? 如何使用AngularJS实现此自动完成功能?

To make it as generic as possible, you could go with a directive approach. 为了使其尽可能通用,可以采用指令方法。 See this Plunker 看到这个柱塞

myApp.directive('completeEmailDomain', function(){  
 return {
    scope: {
       domain: '@',
       email: '='
    },
     restrict: 'E',
     template: '<input ng-model="email" ng-keyup="autoCompleteEmail($event);" />',
     link: function($scope, elem, attr) {
          $scope.autoCompleteEmail = function($event){
            console.log($scope.email);
            if (($event.keyCode === 48 || $event.keyCode === 50) && $event.srcElement.value.slice(-1) === "@") {
              $scope.email += $scope.domain;
            }
          }
     } 
 }; 
});

In there, I created a directive called "complete-email-domain". 在其中,我创建了一个名为“ complete-email-domain”的指令。 It takes in the domain and the email and will automatically update the model once the user enters a '@'. 它接受域和电子邮件,一旦用户输入“ @”,它将自动更新模型。 This should be completely re-usable and should scope the actual handling of changing the value to the new directive. 这应该是完全可重用的,并且应将更改值更改为新指令的实际处理范围。

Perhaps an improvement would be to just store the domain on the user object and pass along the user to the directive. 也许会有一个改进,就是将域存储在用户对象上,然后将用户传递给指令。 Then you can just access the "domain" and "email" properties in a very similar fashion. 然后,您可以以非常相似的方式访问“域”和“电子邮件”属性。

You can pass the user variable as a parameter in the method so it can be reusable: 您可以在方法中将user变量作为参数传递,以便可以重用:

 <input ng-model="user.email" ng-keyup="autocompleteEmail($event, user);">

And take care of that parameter: 并注意该参数:

  $scope.autocompleteEmail = function($event, user) {
       if (($event.keyCode === 48 || $event.keyCode === 50) &&        $event.srcElement.value.slice(-1) === "@") {
  // @ symbol is typed - completing email
  user.email  += "mycompany.com";
}

Try this plunker 试试这个矮人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM