angular using ui-select2 to assign an object as a model property -


hypothetical example illustrate problem having using angular-ui select2. let's have screen want edit "game" model. game, among other things has players. want able set players via select2 drop down menu. here's example code: app.js

$scope.getgamepromise().then(function(results) {     $scope.game = results;     console.log(game.players); //prints [{name:'joe',age: 15},{name:'sally',age:16}] });  $scope.players = [     {         name: 'joe',         age: 15     },     {         name: 'fred',         age: 14     },     {         name: 'sally',         age: 16     },     {         name: 'lucy',         age: 13     } ] 

view.html

<select ngmodel="game.players" ui-select2 multiple>     <option ng-repeat="player in players" value="player">{{ player.name }}</option> </select> 

when want save 'game' object, send game object server. server expecting game.players array of objects. however, being sent string. moderately familiar angular, , new select2. how can select2 set game.players array of objects instead of strings?

i guess find solution or don't have problem anymore. anyway post proposal:

use

<input> 

instead of

<select> 

example:

<input type="hidden" ui-select2="playerscfg" ng-model="players"/> 

and following configuration:

$scope.playerscfg = {         multiple: true,         allowclear: true,         data: { results: player.query(), text: 'name' },         formatresult: function(item) {             return "<div class='select2-user-result'>" + item.name + "</div>";         },         formatselection: function(item) {             return item.name;         }     };  player.query() 

is resource returns list of player containing name (name) , identifier (id)

hope somebody!


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -