1

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.

1
  • 1
    Why not use ng-options instead of ng-repeat? Commented Sep 17, 2015 at 10:25

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

if your outlet variable is a string instead of object, the best way to convert to object is using JSON.parse(outlet)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.