Referencing an example from Angular documentation: this link, specifically the section on "Using ngValue to bind the model to an array of objects."
In the index.html file:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8>
<title>Example - example-select-ngvalue-production</title>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
<script src="app.js"></script>
</head>
<body ng-app="ngvalueSelect">
<div ng-controller="ExampleController">
<form name="myForm">
<label for="ngvalueselect"> ngvalue select: </label>
<select size="6" name="ngvalueselect" ng-model="data.model" multiple>
<option ng-repeat="option in data.availableOptions" ng-value="option.value">{{option.name}}</option>
</select>
</form>
<hr>
<pre>model = {{data.model | json}}</pre><br/>
</div>
</body>
</html>
The app.js file:
(function(angular) {
'use strict';
angular.module('ngvalueSelect', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.data = {
model: null,
availableOptions: [
{value: 'myString', name: 'string'},
{value: 1, name: 'integer'},
{value: true, name: 'boolean'},
{value: null, name: 'null'},
{value: {prop: 'value'}, name: 'object'},
{value: ['a'], name: 'array'}
]
};
}]);
})(window.angular);
Check out the Plunker here: Plunker Link
The issue at hand is that when a user selects an item from the drop-down, instead of the object being passed as Object, it displays "[object Object]" in the ng-model of the select.
Angular suggests using ng-options instead of repeating options individually, but extra logic like ng-style needs to be applied differently for each option in this case.
So, the question remains: How can we ensure that the selected object is passed correctly as an Object in the ng-model of the select element?