To make this as general as possible, you can go with a directive approach. See 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 it, I created a directive called "full email address". It accepts the email address in the domain and automatically updates the model as soon as the user enters "@". This should be fully reused and should cover the actual handling of the change in value to the new directive.
Perhaps an improvement would be to simply save the domain in the user object and pass it in accordance with the directive. Then you can simply access the "domain" and "email" properties in a very similar way.
sourdoughdetzel
source share