简体   繁体   中英

AngularJS select value of property instead of object

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>

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>

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM