簡體   English   中英

當ng-model和ng-options引用不同的對象時,如何使用ng-init在選擇框中選擇默認值

[英]How to use ng-init to select a default value in a select box when the ng-model and the ng-options are referencing different objects

我有一個遍歷listOfThings的選擇框,並且該模型具有currentThing屬性。 選擇框正確綁定到currentThing

但是,我無法通過ng-initlistOfThings的默認值設置為currentThing 以下未設置默認值。

 <select ng-options = "someThing as someThing.name for someThing in controller.listOfThings"
                                ng-model = "controller.currentThing"
                                ng-init = "someThing = controller.currentThing"
                                ng-change = "controller.changeThing(controller.currentThing)"/>

我輸出someThingcurrentThing的值,它們是相同的。

有任何想法嗎? 謝謝!

而不是在DOM中使用,您應該在控制器中分配默認值

喜歡:

$scopr.controller.currentThing = $scopr.controller.listOfThings[0];// first element or as your need

然后不需要

ng-init = "someThing = controller.currentThing"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM