2016-08-26 122 views
2

我有一个select其值是一个简单的对象。选择不显示其默认值

显示为option的内容是此对象的属性,但值是对象本身。

我将此模型设置为适当的对象,为此select设置默认值。

select不显示任何选定的值,而模型设置正确。

这里有3点不同的方法我试过:

Current value of representative.title : {{representative.title}} <br/><br /> 
<div class="form-group"> 
    <select ng-model="representative.title" 
      ng-options="title.name for title in titles" 
      ng-disabled="$parent.isDisabled" 
      class="form-control"> 
    </select> 
</div> 

<br /> 

<div class="form-group"> 
    <select ng-model="representative.title" 
      class="form-control"> 
    <option ng-repeat="title in titles" 
      value="{{title}}"> 
       {{title.name}} 
    </option> 
    </select> 
</div> 

<br /> 

<div class="form-group"> 
    <select ng-model="representative.title" 
      ng-options="title as title.name for title in titles" 
      ng-disabled="$parent.isDisabled" 
      class="form-control"> 

    </select> 
</div> 

而且在我的控制器:

$scope.titles = [{"name": "Monsieur"},{"name": "Madame"}]; 
$scope.representative = {"title": {"name": "Monsieur"}, "otherField": "otherValue"}; 

为什么不是我的选择显示的默认值?

我该如何解决?

(顺便说一句,我使用的角度1.2.28)

+2

___'{}!== {}'___ – Rayon

+0

可能的答案在这里? http://stackoverflow.com/questions/29407513/angularjs-dropdown-not-showing-selected-value – Simo

+0

可能'ng选项'与'标签为'可能会帮助你... – Rayon

回答

3

$scope.representative = {"title": $scope.titles[0], "otherField": "otherValue"};

这将是更准确的,因为你需要通过包含在你的选择数组对象的一个​​参考。

+0

是的,我认为这是这个问题。但是'title'实际上是从一个服务中设置的,所以我需要对它做一些处理以使其等于'titles'的索引,这就是我现在要尝试的 – Ellone

+0

是的,就是这样,它工作正常当我们将它作为参考传递时(除了第二个选择,这可能不是一个角度很好的方法) – Ellone