简体   繁体   English

匹配两个文本框的值

[英]matching values of two textboxes

I've two text boxes with different ng-models . 我有两个具有不同ng-models文本框。 They fill up using a $http.get request on click of a button. 单击按钮后,将使用$http.get请求填充它们。

Lets say: 可以说:

<input type="text" ng-model="name.title" />
<input type="text" ng-model="name.surname" />

These get filled up just fine from my JSON data. 从我的JSON数据中可以很好地填充这些内容。

The value of my name.title can sometimes be like "abc (123)". 我的name.title的值有时可能类似于“ abc(123)”。 If a user edits this, I want the name.surname to become the part inside the (). 如果用户对此进行编辑,则希望name.surname成为()中的一部分。 If the user removes "abc (123)" and just types in 123, then 123 should reflect in name.surname . 如果用户删除“ abc(123)”并仅键入123,则123应该反映在name.surname

I've tried various combinations using ng-blur and ng-change but nothing so far has worked for me. 我已经尝试了使用ng-blurng-change各种组合,但是到目前为止,对我来说都没有用。

Its just one two textboxes so no point writing a new directive. 它只是一个两个文本框,因此编写新指令毫无意义。

How do I do this? 我该怎么做呢?

Any help will be appreciated. 任何帮助将不胜感激。 :) :)

I asume that your using angular 1.x. 我假设您使用的是1.x角度。

You could have a watch over name. 您可以监视名字。 something like in you 像你里面的东西

 $scope.$watch('[name.title, name.surname]', function(newValue, prevValue) { if (newValue === prevValue) { return; } //Here you put your logic and update the models. }); 

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

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