/* global angular, StellarSdk */

angular.module('app')
.directive('uniqueName', function (Contacts, Wallet) {
	'use strict';

	const names = getNames();

	function getNames() {
		const nameList = Wallet.accountList.map(account => account.alias);
		const contacts = Contacts.getNames();
		return new Set([...nameList, ...contacts]);
	}

	return {
		require: 'ngModel',
		link: function (scope, element, attributes, ngModel) {
			ngModel.$validators.uniqueName = function (modelValue) {

				if ((attributes.uniqueName) &&
					(attributes.uniqueName === modelValue)) {
					return true
				}
				return !names.has(modelValue);
			};
		}
	};

});

/* global angular, StellarSdk */

angular.module('app')
.directive('uniqueName', function (Contacts, Wallet) {
	'use strict';

	function getNames() {
		const nameList = Wallet.accountList.map(account => account.alias);
		const contacts = Contacts.getNames();
		return new Set([...nameList, ...contacts]);
	}

	return {
		require: 'ngModel',
		link: function (scope, element, attributes, ngModel) {
			ngModel.$validators.uniqueName = function (modelValue) {

				if ((attributes.uniqueName) &&
					(attributes.uniqueName === modelValue)) {
					return true
				}
				return !getNames().has(modelValue);
			};
		}
	};

});

Update name lists on each call
Update name lists on each call
