2016-08-26 175 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