2016-08-16 110 views
0

我正在使用ng-options來創建選擇選項。 CA.JSON文件包含所有控件值/選項。如何設置默認值,請選擇控制選項?

這裏在爲VehicleYear選項我想添加默認值,請選擇選項。

<select> 
<option value="">Please select</option> 
<option value="2017">2017<option> 
... 
<select> 

我tryed如下,但它不工作

<select ng-change="updateMakeList" custom-required="true" ng-options="ans._value as ans._promptText for ans in questions[$state.current.name].VehicleYear.QuestionData._answerOptions" ng-model="answers.VehicleYear" ng-required="queObj._required" class="form-control {{queObj._pageAttributes.cssclass}}" name="{{questions[$state.current.name].VehicleYear.QuestionData._attributeName}}" id="{{questions[$state.current.name].VehicleYear.QuestionData._attributeName}}" data-que-obj="questions[$state.current.name].VehicleYear.QuestionData" select-control-dir setMake custom-required> 
           <option value="">Please select</option> 
          </select> 

https://plnkr.co/edit/aV65Nab9U9I6YlK2g4sY?p=preview

+0

'<選項值=「」選中>請選擇' –

+0

請選擇選項未顯示在列表中。如何添加ng選項。 –

+0

如何更改'answers.VehicleYear'?你的代碼非常難以調試。在app.js中,如果我添加了'$ rootScope.answers = {PC:12345,VehicleYear:null};'它被初始化爲2017 ... – gyc

回答

0

嘗試下面的代碼,用來選擇

<select ng-init="selectModal= Please Select" 
     ng-model="selectModal" 
     ng-options="option.name for option in options"> 
</select> 

//其中的選項是你的選擇陣列你的情況我認爲它的問題[$ state.current.name] .VehicleYear.QuestionData._answe rOptions

你還可以通過添加選項如下

<option style="display:none" value="">Please select</option> 

嘗試當試圖值分配給選擇這不是在選擇陣列或集合此問題時

Updated code without style

+0

問題[$ state.current.name] .VehicleYear.QuestionData._answerOptions僅包含年份列表。在用戶界面上,我想添加,如果ng模型沒有它應該選擇的節點的值,請選擇作爲第一個選項。 –

+0

是否在更新後嘗試我的代碼「selectModal =請選擇」或添加<選項值=「」選中> - 請選擇您的顏色 -? –

+0

是的,我試過它不工作。 –