Reputation: 163
HTML:
<select ng-model="outlet">
<option ng-repeat="outlet in outlets" value='{{outlet}}'>
{{outlet.name}}</option></select>
Now, In my controller:
console.log(outlet)
console.log(outlet._id)
var selectedoutlet = {"_id":"554c930c79d26026307a5a9d","name":"Model Town","city":"Default"};
console.log(selectedoutlet);
console.log(selectedoutlet._id)
Output:
{"_id":"554c930c79d26026307a5a9d","name":"Model Town","city":"Default"}
undefined
{_id: "554c930c79d26026307a5a9d", name: "Model Town", city: "Default"}
554c930c79d26026307a5a9d
Why is it that I can not access properties of outlet object?How do I fix this? I can see it is because my object properties are strings but how do I get around this? I certainly do not want to loop and match string. I just need to access object properties.
Upvotes: 1
Views: 82
Reputation: 1174
Use ng-options to have the model as json of the selected element, in case of using ng-repeat, the model is bind to the option value as a string. Here is how it should be:
<select ng-model="outlet" ng-options="outlet.name for outlet in outlets" ng-change="display()">
</select>
Check this for further explanation.
Upvotes: 2
Reputation: 1634
if your outlet variable is a string instead of object, the best way to convert to object is using JSON.parse(outlet)
Upvotes: 1