Reputation: 2395
I'm trying to use ng-options
inside ng-repeat
, but seems I'm missing something bwcause I do not see any values populated into the select control.
Here is the html code:
<div ng-controller="testCtrl">
<table border="1" style="width:300px">
<tr>
<th>id</th>
<th>name</th>
<th>age</th>
<th>select</th>
</tr>
<tr ng-repeat="person in persons">
<td>{{ person.id }}</td>
<td>{{ person.name }}</td>
<td>{{ person.age }}</td>
<td>
<select ng-model="person.chosenNumber" ng-options="suggestion.number as suggestion.destination for suggestion in person.suggestedPhones"></select>
<button ng-click="showSelected()">Ok</button>
</td>
</tr>
</table>
</div>
Js code:
var testModule = angular.module('myApp', []);
function testCtrl($scope) {
$scope.persons = [
{'id': 1, 'name': 'omer', 'age': 35, 'suggestedPhones': {'destination': 'home', 'number': '0544317259'} },
{'id': 2, 'name': 'noam', 'age': 32, 'suggestedPhones': {'destination': 'home', 'number': '036024607'} },
{'id': 3, 'name': 'dafna', 'age': 28, 'suggestedPhones': {'destination': 'home', 'number': '0522318779'} }
]
$scope.showSelected = function () {
alert(1);
}
}
Upvotes: 2
Views: 4602
Reputation: 1
HTML:
<div ng-controller="testCtrl">
<table border="1" style="width:300px">
<tr>
<th>id</th>
<th>name</th>
<th>age</th>
<th>select</th>
</tr>
<tr ng-repeat="person in persons">
<td>{{ person.id }}</td>
<td>{{ person.name }}</td>
<td>{{ person.age }}</td>
<td>
<select ng-model="person.chosenNumber" ng-options="suggestion.number as suggestion.destination for suggestion in person.suggestedPhones"></select>
<button ng-click="showSelected($index)">Ok</button>
</td>
</tr>
</table>
</div>
Javascript:
var testModule = angular.module('myApp', []);
function testCtrl($scope) {
$scope.persons = [
{'id': 1, 'name': 'omer', 'age': 35, 'suggestedPhones': {'destination': 'home', 'number': '0544317259'} },
{'id': 2, 'name': 'noam', 'age': 32, 'suggestedPhones': {'destination': 'home', 'number': '036024607'} },
{'id': 3, 'name': 'dafna', 'age': 28, 'suggestedPhones': {'destination': 'home', 'number': '0522318779'} }
]
$scope.showSelected = function (data) {
alert($scope.persons[data].chosenNumber);
}
Upvotes: -1
Reputation: 40298
Either the suggestedPhones
is wrong (should be array) or you should use the label for (key , value) in object
(ref) variant of ng-options
. Try the following, if it is indeed what you want:
$scope.persons = [
{'id': 1, 'name': 'omer', 'age': 35, 'suggestedPhones': [{'destination': 'home', 'number': '0544317259'}] },
{'id': 2, 'name': 'noam', 'age': 32, 'suggestedPhones': [{'destination': 'home', 'number': '036024607'}] },
{'id': 3, 'name': 'dafna', 'age': 28, 'suggestedPhones': [{'destination': 'home', 'number': '0522318779'}] }
]
(The suggestedPhones
have become arrays.)
(UPDATE RELEVANT TO COMMENT) Getting the selected person and phone data is as simple as:
<button ng-click="showSelected(person)">Ok</button>
And in the JS:
$scope.showSelected = function (person) {
console.log(person);
}
Check it out: http://jsfiddle.net/C5jK9/
Upvotes: 4