簡體   English   中英

AngularJS選擇不在范圍內的ng-model值

[英]AngularJS select ng-model value not in scope

我將普通選項用於選擇標簽,因為我只有在滿足條件時才需要顯示幾個選項。 我需要控制器中選定下拉列表的值來執行其他操作,但是問題是我沒有得到范圍內的值。

<select id="selectView" ng-model="selectedView" ng-init="selectedView='plain'" ng-change=resultViewChanged()>
  <option value="plain" >Plain</option>
  <option value="grid" ng-if="user.gridView" >Grid</option>
  <option value="box" ng-if="user.boxView">Box</option>
</select>`

現在,在控制器中,我只是嘗試獲取selectedView的值。 我將下拉列表更改為網格,並在以下兩個警報下進行了嘗試,但都未在警報框中提供值網格。 如何獲取控制器中所選選項的值?

$scope.resultViewChanged = function() {
        alert($scope.selectedView.value); //Tried this undefined
        alert($scope.selectedView);

}

感謝您的幫助

所選值應存儲在ng-model中定義的$ scope.selectedView變量(后沒有.value)中。

所以這:

alert($scope.selectedView);

應該在下拉菜單中提醒所選的值。

這個怎么樣?

alert(document.getElementById('selectView').value);

暫無
暫無

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

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