0

So, I'm looking to create my first angular directive for a validation check. Essentially I want to ensure an user name is unique. This answer is perfect for my first pass, and works as expected for creating a new user.

I'd like this also to work for users who want to change their user name. On the server side, I would write code to check that the user name doesn't exist in the database for any user other than the current user. In other words, if a user is modifying their profile, and the form has a group of fields that include user name, I don't want to form to be invalid if the username already exists, since the reason it exists is that the current user already has it.

So what I would like to do is pass the userId (an integer PK) as a parameter in the ajax call in addition to the user name. The userId exists on the $scope, but I don't know how to modify the directive to allow me to pass additional information.

I would imagine the markup would look something like the following?

    <input type="email" ng-model="userEmail" 
           name="userEmail" required unique-email="userId"/>

1 Answer 1

1

Since the uniqueEmail directive does not create a new scope, you can pass the name of the property as shown (unique-email="userId") and then use $eval in your link function:

var userId = scope.$eval(attrs.uniqueEmail);

Note that I'm assuming the link function parameter is named attrs (not attr).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.