简体   繁体   English

Angular JS:设置部分表单字段值?

[英]Angular JS: Set partial form field value?

I've received an odd request. 我收到了一个奇怪的要求。

The client would like to have the country code of a phone number pre-populated in the "phone" field in the form. 客户希望在表格的“电话”字段中预先填写电话号码的国家/地区代码。 Easy enough to do with ng-model="0 33 " ... 使用ng-model="0 33 " ...非常容易

在此处输入图片说明

except when a user tabs into the field, the entire contents will be selected, and overtyped. 除非用户将标签页拖入该字段,否则将选中所有内容并改写。

can anyone think of a way to set it up so tabbing into the field will simply place the insertion point after the already-existing contents from the ng-model when the user tabs into the field, so the user can simply continue typing the phone number? 任何人都可以想到一种设置方式,因此在用户进入该字段时,进入该字段将仅将插入点放在ng-model已存在的内容之后 ,因此用户可以继续输入电话号码?

So that it would be like 0 33 | 这样就好像0 33 |

You can run the cursor to the end of the input using: 您可以使用以下命令将光标运行到输入的末尾:

onfocus="this.value = this.value;"

Example: 例:

 var app = angular.module('TestApp', []); app.controller("testCtrl", function ($scope) { $scope.foo = "0 33 "; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="TestApp" ng-controller="testCtrl"> <input ng-model="foo" onfocus="this.value = this.value;" /> </div> 

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

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