Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

Let me first say that I know that what I am asking for is not a desired pattern, Its just something that I need as a temporary patch.

I want to be able to bind a single input element to two "places" in the model. e.g.

Suppose I have an input for entering a username and I want it to populate 3 fields in the model: user,username and user_name.

I don't care about what happens in cases of collision... i.e. after assigning model.user='aaa'; andmodel.username='bbb'` any consistent behavior is o.k. for me.

The simplest thing I can think if is binding to user and then watching user and updating username and and user_name.

maybe I can create a directive that will copy over from the ng-model to its own targets ...

Is there any smart simple fast way to use ng-model for this or any other trick I can / should use?

UPDATE - To try and satisfy the curious.

I need this for the following scenario:

NOTE:It is something that was NOT designed for angular, and I want to move to angular but can not conver the whole thing at once.

Consider a form that has a select for an action Then several Inputs for different parameters of different actions, some fields are common to more the a single action. The fields are tagged with classes that represent the actions they belong to, and JQuery is used to hide and show them based on the selected action and to collect the values from all the fields that belong to the current action for using them when the action needs to be performed.

now I want to start separating the actions to different independent forms and different models, It seemed to me that the easiest thing would be if for the transitional phase I was able to set a single input to update more then a single model.

share|improve this question
    
I agree, using a $watch for this scenario is probably easiest – pixelbits Jun 23 '14 at 14:11
    
Or use a function on the controller to be the onchange for the input and in the function you assign the 3 variables to the input value. – Tacoman667 Jun 23 '14 at 14:13
    
I am interested in why you want to do this! – Matt Way Jun 23 '14 at 14:21
    
Matt, I need this because I want the same form control to participate in two different collections of data. I will add an update with a more detailed explanation. – epeleg Jun 24 '14 at 7:17

1 Answer 1

up vote 1 down vote accepted

I'm not entirely sure I am clear on what you are asking. Is this what you mean?

   <input ng-model="user" ng-change="user_name=username=user" type="text" /><br/>
   User: {{user}}<br/>
   Username: {{username}}<br/>
   User_name: {{user_name}}

http://plnkr.co/edit/tpl:8rFfZljYNl3z1A4LKSL2?p=preview

share|improve this answer
1  
I like your solution very much. I think this is is exactly what I was looking for (even though it was quite a while ago). – epeleg Apr 20 at 6:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.