在角度js 1中访问文本框值的问题?

时间:2017-01-16 13:29:23

标签: angularjs angularjs-1.5

我在ng-keyup上调用函数并尝试传递一个实际上没有通过的文本框值并获得" undefined" 变量

html代码

<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>

功能的控制器代码

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;
          }

1 个答案:

答案 0 :(得分: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> 

您可以直接传递ng-model本身,如下所示:

ng-keyup = "vm.firstLetterCaps('fname',vm.contact.fname)"