2013-08-26 97 views
1

我有一個JSON對象,如下Angularjs - 使用NG選項

keyword = { 
    application: ["Athena","EWindow","EWS","FACT","FTP","Hardware","PEN","Hermes","Infrastructure","LNG Tracker","M2M","Maintenance","Microsite","Oracle","Platts.com","PMC","PTO Task","Sametime","Third Party Data","User Error","Vendor","Web Channels","XML-DD","Customer Request","Mark Logic","SBB","Market Engagement Form","Lotus Notes Database","Oracle11i","External News","Stringers","PRP","Kingsman","Hermes-1"], 
    incidentType: ["Publishing Failure","Brand Damage","Security Failure","Content Creation Failure","Internal failure","External Failure","System Failure","Operational Failure","Editorial Failure","Content inaccuracy","Process Failure","Application Issue","Infrastructure Issue","User Issue","Other","Resend","Data Send validation","Customer issue"], 
    incidentEnvironment: ["Production","Non-Production","Others"], 
    incidentPriority: ["Low","Medium","High","Critical"], 
    incidentLocation: ["Asia Pacific","Europe","New York","New Jersey","Houston","Washington DC","Others"], 
    incidentStatus: ["Initiation","Work In Progress","Completed","On Hold","Closed","Cancelled"], 
    incidentCategory: ["ActiveX","Checkin","Checkout","CKEditor","Corrupt Story","Delete Story","Delivering Content","Download-Upload","Filter","Graph-Rich Media","IE Cache","IE Setting","Indicia","InDesign","Infrastructure","Ingest(Table)","Other","PDF","Publish","Search","Table","User Issue"], 
    incidentTicketTools: ["BMC Remedy"], 
} 
<div class="col-lg-3"> 
    <select name="txt_inc_Status" class="form-control input-sm" required> 
     <option selected disabled> 
      -- Select Status -- 
     </option> 
     <option ng-repeat="incidentStatus in keywords.incidentStatus" value="{{incidentStatus}}"> 
      {{incidentStatus}} 
     </option> 
    </select> 
</div> 

我需要選擇框來填充這些值填充選擇框。例如:Keywords.application in application選擇框等。

早些時候我用ng-repeat來構造選項。但我知道不建議這樣做。

所以我想用ng-option,但在填充這個時遇到困難。有人可以幫助我嗎?

另外我需要爲這些選擇框選擇一個默認值(隨機)。這怎麼可能實現。當我用ng-repeat

+1

在keyword.incidentStatus「'ins中添加'ng-options =」incidentStatus找到你的'select'標籤。 – tymeJV

+0

請注意,你的json對象的標題是'keyword',但是你的綁定html顯示'keywords',不知道這是否是一個錯字 – jnthnjns

+0

我嘗試了下面的代碼,類似於你的代碼,但它的工作。這是「關鍵字」

Saravanan

回答

2

它沒有工作,你可以使用

<div ng-repeat="(key, value) in keywords"> 
    <select ng-options="item for item in value" ng-init="index = getRandomIndex(value)" ng-model="value[index]">{{item}}</select> 
</div> 

連同一個函數來獲取隨機指數當前陣列

$scope.getRandomIndex = function(item){ 
    var index = Math.floor((item.length * Math.random())); 
    return index; 
} 

DEMO

+0

我嘗試下面的代碼類似於你的,但它的力量的工作。

Saravanan

+0

@saravanan這就是爲什麼我爲你創建了一個演示。你不應該等待答案,試着自己解決。這個工作演示包含了你理解它所需要的一切。 – zsong

+0

謝謝。我也嘗試過,但沒有ng-repeat。我不想用ng-repeat構造選擇框,因爲它將位於html中的不同位置。我只想填充json的選項。我在jsfiddle下面試過,它的功能很強大。 Saravanan