简体   繁体   中英

matching values of two textboxes

I've two text boxes with different ng-models . They fill up using a $http.get request on click of a button.

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.

The value of my name.title can sometimes be like "abc (123)". If a user edits this, I want the name.surname to become the part inside the (). If the user removes "abc (123)" and just types in 123, then 123 should reflect in name.surname .

I've tried various combinations using ng-blur and ng-change but nothing so far has worked for me.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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