2016-08-26 5 views
2

私は値が単純なオブジェクトであるselectを持っています。デフォルト値を表示しないで選択してください

optionとして表示されるコンテンツはこのオブジェクトのプロパティですが、値はオブジェクトそのものです。

モデルを適切なオブジェクトに設定して、このselectのデフォルト値を設定しています。

しかし、selectは、モデルが適切に設定されている間は値を選択していません。

ここに私が試した3種類の方法があります:

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

なぜデフォルト値を示す私の選択ではありません。

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> 

そして、私のコントローラで

どうすれば修正できますか?

(ところで、私は角1.2.28を使用しています)

+2

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

+0

可能な回答はこちらですか? http://stackoverflow.com/questions/29407513/angularjs-dropdown-not-showing-selected-value – Simo

+0

'ng-options'は' label as 'で助けてくれるかもしれません... – Rayon

答えて

3

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

あなたはoptions配列に含まれるオブジェクトの1の参照を渡す必要があるとして、それはより正確になります。

+0

さて、それはここの問題だと思います。しかし、 'title'は実際にはサービスから設定されているので、' title'のインデックスと同じにするために少し扱う必要があります。それは私が今しようとしているものです – Ellone

+0

はい、それは働いていました私たちがリファレンスとして渡すとき(2番目の選択を除いて、それはおそらく角度をつけて行うのが良い方法ではないでしょう) – Ellone

関連する問題