0

I have created a multiselect with autocomplete directive its mapping the selected list to the ng-model of the directive . I want to get a single field value from the ng-model and assign that to another ng-model . I am not able to do that . If I am passing the whole ng-model value and assigning to another ng-model its working fine but with single field value it is not working.

Please find the link to the fiddler for the code -

https://jsfiddle.net/719x1rbs/11/l

<div ng-controller="TestCtrl">


    <typeahead data-ng-model="selecteds" ng-change="emailId=selecteds.email" items="contacts" displaytag="name" displayitem="name">
    </typeahead>
    <input type="hidden" ng-model="emailId" />
    {{emailId}}
  </div>

I am not able to run the code in fiddle but its a running code in my system.

Can anyone please help me on how I can pass one field in an ng-model to another ng-model. In the {{emailId}} I should be getting the value '@pacman.com'.

Viplock
  • 3,259
  • 1
  • 23
  • 32
Sonam
  • 31
  • 1
  • 5
  • provide some more elaboration , what you want and where its gonna reflect the changes. @sonam – Viplock Jul 29 '16 at 09:56
  • In the ng-model attached to the directive I am getting the values as an array. At the backend I do not want to send the complete array of the selected items. I just want to send like say in the above example the emailId so I am taking a hidden variable and mapping the ng-model value of the directive to this hidden variable ng-model onchange but its not working. I want to pass the value in the backend as {'red@pacman.com','pink@pacman.com','cyan@pacman.com'} . Also I ahve attached the fiddler code for the same. – Sonam Jul 29 '16 at 10:19
  • Ok, I got it, for one thing i wanna correct you its not "fiddler" its "fiddle" fiddler is some other tool. – Viplock Jul 29 '16 at 10:58

1 Answers1

0

Actually the problem with your code is, you want to get email id from the selected items, by doing selecteds.email but selecteds is not a object with property email its a Array of selected objects. So you should use Two way binding property of Angular and directly use the selecteds Array of object and iterate it to get the separate Array of email Ids. On some specific event . Because in your case ng-change event not gonna fire every time value of Model will change because its a custom directive you can go through Question/Answer. So make sure you fire a proper event for your need full.

Community
  • 1
  • 1
Viplock
  • 3,259
  • 1
  • 23
  • 32