0

I have a Jasmine test for my angular controller with the following function:

function compileDirective() {
        var body = '<div class="col-sm-4">' + 
'<input id="email" type="email" ng-model="form.email" value="[email protected]" field-match="emailsMatch()">' + '</div>';
        var tpl = '<form name="myForm">' + body  + '</form>';

        inject(function($compile) {
            var form = $compile(tpl)(scope);
            elem = form.find('div');
        });
        function emailsMatch() {
          //do some logic in here, depending on the value of the email field
        }

        scope.$digest();
    }

I want to be able to create another function inside the compileDirective function (emailsMatch()) which looks at the value in the input field and does some logic using it. I don't know how to access the input field though. I have a handle to elem which is the entire html starting with the . How can I look at the value of the input field?

1 Answer 1

1

To access emailsMatch function,you should make it a part of the scope.On the other hand, if you have ng-model="form.email" written in your markup, I assume you have form(with emailproperty inside) property in your scope. So you need something like this:

scope.emailsMatch = function() {
   console.log( scope.form.email ); //should print value of the <input>
};
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.