1

I have a directive for validating creditcard luhn, and length. As a validator of the number it works great. The algorithm used is based off of a jquery plugin (sans jquery) that also returns the card type to me. The ending result is something like

{
    luhn_valid: true,
    length_valid: true
    card_type: {
        name: 'visa'
    }
}

So it gives me the card type name. I want to assign this to a different model variable so that I can pass both the card type and card number to REST api to add the card. The problem is I do not know how to access a model that is not declared associated with the directive.

<input type="text" name="cardNumber" ng-model="cardCtrl.form.inputs.cardNumber" 
    credit-card-validator card-type-model="cardCtrl.form.inputs.cardType">

In the example above I add an extra attribute to pass the model name to the directive. I have passed $scope to the directive and can see cardCtrl as a child of $scope, so I feel I should be able to get to the model from it, but I am not sure the best way to do it.

Thoughts on the best way to get access to this other model?

2
  • Can you please add a jsFiddle/plnkr for your code? Commented Aug 13, 2015 at 19:51
  • how is this card-type-model declared in your directive code? Commented Aug 13, 2015 at 20:12

1 Answer 1

2

In the directive controller, by calling $scope.$parent.'ParentControllerVariableName', you can get the value of the controller scope variable from the directive, but this is not a good practice.

You can create a factory and set your value in factory method and define a get method. Inject this factory into your directive and get the value from your factory.

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

1 Comment

Thank you this was so simple and exactly what I needed.

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.