AngularJS选择属性值而不是对象

时间:2014-07-17 14:40:26

标签: javascript angularjs angularjs-directive

我想将ng-model与属性的字符串值绑定,而不是与完整对象绑定。 http://jsfiddle.net/arkomahmud/753YB/2/

我的HTML代码如下所示

<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 个答案:

答案 0 :(得分:2)

http://jsfiddle.net/zq53z/ group.Name是你的$ scope.Groups中的一个字符串,你有名字而不是名字

<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 :(得分:1)

你可以这样做:

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

但这会打破group.name.Value的条件。