简体   繁体   English

如果文本框值是AngularJs中下一个文本框的自动输入,如何隐藏错误

[英]how to hide the error if a textbox value is an auto input to the next textbox in AngularJs

I have a form in which I have a textbox for providing nationality and once I input the country name, corresponding country code is auto input to the next textbox which is a contact number field. 我有一个表格,其中有一个提供国籍的文本框,输入国家名称后,相应的国家/地区代码会自动输入到下一个文本框,即联系电话字段。

HTML 的HTML

 <input type="text" name="country" class="form-control" typeahead-on-select="change(nationality)" ng-model="nationality" typeahead="country  as country.contName for country in countries |  filter:$viewValue | limitTo:10" ng-readonly={{viewMode}} placeholder="Country Name" required />
 <input type="text" class="form-control" name="number" ng-model="number"    ng-pattern="/^[ ()+]*([0-9][ ()+]*){12}$/" placeholder="Contact No" required/>
    <span ng-show="myForm.number.$error.pattern"><span style="color:red">Please Enter 10 digit Number </span>
</span>

JS JS

$scope.nationality='';
$scope.countries   = [{ 'contName': 'India', 'code':'+91'}, { 'contName':   'Brazil',      'code':'+55'},
                          { 'contName': 'Pakistan',   'code':'+92'}, { 'contName':  'New Zealand', 'code':'+64'},
                          { 'contName': 'Canada',      'code':'+01'}, { 'contName': 'France',      'code':'+33'}];

$scope.change=function()
            {
                $scope.number = $scope.nationality.code;

            };

The problem is, when I select a country, corresponding country code auto populated in contact number field and it is showing error(Enter 10 digit). 问题是,当我选择一个国家/地区时,相应的国家/地区代码会自动填充在联系电话字段中,并且显示错误(输入10位数字)。 I want to show this error message only after the user gives some input manually or after moving to the next textbox. 我只想在用户手动输入某些内容或移至下一个文本框后才显示此错误消息。 How to do it? 怎么做?

Considering your form has a name xyz . 考虑到您的表单名称为xyz Then after you assigned the number to your form, try calling $scope.xyz.$setPristine(); 然后,在将数字分配给表单后,尝试调用$scope.xyz.$setPristine();

$scope.change = function() {
    $scope.number = $scope.nationality.code;
    $scope.xyz.$setPristine();
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM