3

I want to bind ng-model with string value of a property not with the complete object. http://jsfiddle.net/arkomahmud/753YB/2/

My HTML code looks as follows

<div ng-app='myApp' ng-controller="ArrayController">
    <select ng-model="group.name" ng-options="Group.Name for Group in Groups"></select>
    <div class="col-md-4" ng-show="group.name.Value == 'new'">
        <input class="text-box" type="text" name="NewValue" ng-model="newValue" />
        <button ng-click="add(newValue)">Add</button>
    </div>
      <p>Here I want Value Only[Not object]: {{group.name}}</p>
    <p>Now I get it in this way: {{group.name.Name}}</p>
    <p><b>But I want group.name to be string not object</b></p>
</div>

2 Answers 2

2

You can do:

 <select ng-model="group.name" ng-options="Group.Name as Group.Name for Group in Groups">

But that will break the condition on group.name.Value.

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

3 Comments

Thanks this is what I wanted. Somehow misunderstood the documentation :D
@Muffin sylwester's answer is probably what you want ;)
Nope :D, I wanted to group.Name to a string and value of Groups coming from a Database table where many other things are stored. I just excluded them from example. My Real array of object is something like [{"Types": "Group", "Name": "GroupA"},{{"Types": "Group", "Name": "GroupB"}}]. In this way I am saving all dropdown options in single table. But I am not sure if it is optimal.
2

http://jsfiddle.net/zq53z/ group.Name is a string as in your $scope.Groups you've got Name not name

<div ng-app='myApp' ng-controller="ArrayController">
    <select ng-model="group" ng-options="Group.Name for Group in Groups"></select>
    <div class="col-md-4" ng-show="group.name.Value == 'new'">
        <input class="text-box" type="text" name="NewValue" ng-model="newValue" />
        <button ng-click="add(newValue)">Add</button>
    </div>
    <p>Here I want Value Only[Not object]: {{group.Name}}</p>
    <p>Now I get it in this way: {{group.name.Name}}</p>
    <p><b>But I want group.name to be string not object</b>
    </p>
</div>

1 Comment

Thanks for your answer. But I wanted to bind string value only. That I got from mezes' answer.

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.