2017-05-01 9 views
1

私は非常にうまく動作するこのhtmlを持っています。私のtopicIdは0から7まで表示され、0が最初の項目です。 しかし、ページが読み込まれてからドロップダウンをクリックするまで、私のドロップダウンには何も表示されません。anglejsを使用してドロップダウンメニューにデフォルトのアイテムを持っています

最初の項目がデフォルトとして表示されます。どうすればいいですか?

 <div class="selectTopic"> 
      <select id="cTopic" name="cTopic" ng-model="selectedValue"> 
       <option ng-repeat="x in cuc.getData" value="{{x.topicId}}"> {{x.topicDescription}}</option> 
      </select> 
     </div> 
+0

あなたのモデルを設定してください(selectedValue)をオプションのng-repeatコレクションの最初の項目に追加します。 – RamblinRose

+0

どのように?コードサンプルがありますか? – user2320476

答えて

0

代わりにngOptionsを使用する必要があります。次に、デフォルト値にコントローラを介してモデルINIT:

<select id="cTopic" name="cTopic" ng-model="selectedValue" 
    ng-options="item.topicId as item.topicDescription for item in cuc.getData"> 
</select> 

とコントローラで:

$scope.selectedValue = "topicId1"; 

例を参照してください:

var app = angular.module('TestApp', []); 
 

 
app.controller("testCtrl", function ($scope) { 
 
\t 
 
    $scope.selectedValue = "topicId1"; 
 
    $scope.items = [ 
 
    { 
 
     topicId : "topicId1", 
 
     topicDescription :"topicDescription1" 
 
    }, 
 
    { 
 
     topicId : "topicId2", 
 
     topicDescription : "topicDescription2" 
 
    }, 
 
    { 
 
     topicId : "topicId3", 
 
     topicDescription : "topicDescription3" 
 
    } 
 
    ]; 
 
    
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="TestApp" ng-controller="testCtrl"> 
 
    <div class="selectTopic"> 
 
     <select id="cTopic" name="cTopic" ng-model="selectedValue" ng-options="item.topicId as item.topicDescription for item in items"> 
 
     </select> 
 
    </div> 
 
</div>

0

は、あなたが使用NG-選択必要があります:あなたはプレーンなHTMLを使用している場合は、しかし、https://docs.angularjs.org/api/ng/directive/ngSelected

<div class="selectTopic"> 
    <select id="cTopic" name="cTopic" ng-model="selectedValue"> 
     <option ng-repeat="x in cuc.getData" value="{{x.topicId}}" 
      ng-selected="selectedValue === x.topicId"> 
      {{x.topicDescription}} 
     </option> 
    </select> 
</div> 

を、AngularJSは、ドロップダウンを作成するためのより効率的な方法があり、以下を参照してください。https://docs.angularjs.org/api/ng/directive/ngOptions

+0

これは機能しません。同じ問題 – user2320476

0

示唆した答えをしかし、私が使用する傾向のある代替方法はこれです:

<div class="selectTopic"> 
<select id="cTopic" name="cTopic" ng-model="selectedValue" ng-options="x.topicId as x.topicDescription for x in cuc.getData"> 
    <option value="">Select One ...</option> 
</select> 

Angularは、初期化されていない選択肢に対しては何らかの値で開始する必要があります。したがって、htmlに空白のオプションを指定すると、ユーザーに代わって前提条件を選択せず​​に安全に初期化されます。

+0

別の言語になるアプリケーションで作業しているので、コードで「Select One」がありません。すべてがデータベースから来ています – user2320476

+0

あなたはどういう意味が分かりませんか?モデルに格納される値は空の文字列です(別のものを選択しない場合)。 – chukkwagon

+0

私はそれを試して運がありません。最初の項目が表示されているだけで、ドロップダウンの他の項目はすべて表示されません。 1つを選択してください.. – user2320476