0

I am calling function on ng-keyup and trying to pass a textbox value which is not actually got passed and got "undefined" variable

html code

<md-input-container class="md-icon-float md-block">
                <label >First Name </label>
                <md-icon md-font-icon="icon-account" class="name"></md-icon>
                <input type="fname" id="fname" name="fname" ng-value = "{{vm.form.fname}}" ng-model="vm.contact.fname" ng-pattern="/^[a-zA-Z ]{2,30}$/" type="text" ng-keyup = "vm.firstLetterCaps('fname',vm.form.fname)"  required> 
                <div ng-messages="contactForm.fname.$error" role="alert">
                        <div ng-message="required">
                            <span>First name is required</span>
                        </div>
                        <div ng-message="pattern">
                            <span>First name must be a valid </span>
                        </div>
         </div>
 </md-input-container>

controller code for function

function firstLetterCaps(textboxid, str){
            console.log(textboxid)
            console.log(str)
            // string with alteast one character
              if (str && str.length >= 1)
              {       
                  var firstChar = str.charAt(0);
                  var remainingStr = str.slice(1);
                  str = firstChar.toUpperCase() + remainingStr;
              }
              if()
              document.getElementById(textboxid).value = str;
          }
2
  • Just to clarify, What do you need for an ng-value when you have an ng-model? Commented Jan 16, 2017 at 13:33
  • I already tried it but got undefined value in console. Commented Jan 16, 2017 at 13:54

1 Answer 1

0
 <input type="fname" id="fname" name="fname" ng-value = "{{vm.form.fname}}" 
    ng-model="vm.contact.fname" ng-pattern="/^[a-zA-Z ]{2,30}$/" type="text" 
    ng-keyup = "vm.firstLetterCaps('fname',vm.form.fname)"  required> 

You can directly pass your ng-model itself, like below:

ng-keyup = "vm.firstLetterCaps('fname',vm.contact.fname)"
Sign up to request clarification or add additional context in comments.

1 Comment

@SaMeEr: So basically you want to captialize your first letter in your input field right and also check whether your vm has a contact object in your controller like vm.contact = {}

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.