0

I am trying to get the input value for a form field, but when I use the code below, the value displays as undefined.

Any idea what I'm doing wrong here?

.controller('ContactFormCtrl',
   function (Contacts) {
     var contactForm = this;
     contactForm.contacts = Contacts;
     contactForm.contact = {};
     var mail=contactForm.contact.email;

     contactForm.onchange = function () {console.log(mail);};
 });


<div class="col-sm-6 form-group">
   <label class="control-label" for="email">Email</label>
   <input type="email" id="email" name="email" ng-model="contactForm.contact.email" class="form-control" ng-change="contactForm.onchange()" ng-model-options="{ updateOn: 'blur' }" />
</div>

2 Answers 2

1

Update the controller as :

 .controller('ContactFormCtrl',
  function (Contacts) {
    var contactForm = this;
    contactForm.contacts = Contacts;
    contactForm.contact = {};
    contactForm.contact.email="";
    var mail=contactForm.contact.email;

    contactForm.onchange = function () {console.log(mail);};
 });

Currently, there is no email property with "contactForm.contact" object. So you need to initialize the email property and it will not give you undefined error.

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

Comments

0

As you are using controllerAs syntax then you should use alias there contactForm

<div ng-controller="ContactFormCtrl as contactForm">
 <div class="col-sm-6 form-group">
   <label class="control-label" for="email">Email</label>
   <input type="email" id="email" name="email" ng-model="contactForm.contact.email" class="form-control" ng-change="contactForm.onchange()" ng-model-options="{ updateOn: 'blur' }" />
 </div>
</div>

And the reason it is undefined is you have reinitialized controller contact object after contactForm.contacts = Contacts; which overrides the value of email.

Update

As discussed in chat you want to show email on blur as well as you want to call onchange function on email validate, for that you should have to use combination of directive ng-change with ng-blur then you should get rid off ng-model-options which don't suits your requirement.

Markup

<div ng-controller="ContactFormCtrl as contactForm">
    <form name="myForm">
        <div class="col-sm-6 form-group">
            <label class="control-label" for="email">Email</label>
            <input type="email" id="email" name="email" ng-model="contactForm.contact.email" class="form-control"
            ng-change="myForm.email.$valid && contactForm.onchange()" ng-blur="contactForm.onchange()"/>
        </div>
    </form>
</div>

9 Comments

Thank you. When I remove contactForm.contact = {}; I get the following error: Cannot read property 'email' of undefined
@Ken you should not removed that, you should keep it..could I know contacts and contact are different?
I don't have a good reason for using contact and contacts in my example above. Any suggestions on how to fix my code to resolve the undefined issue?
@ken I don't understand how can contactForm.contact.email will contain any value after initialization of contactForm.contact = {}
are you asking that after changing contactForm.contact.email value it should change in console.log?
|

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.