簡體   English   中英

無法使用angular.js獲取下拉列表的默認選定值

[英]Not getting default selected value of drop down list using angular.js

我沒有使用angular.js獲取下拉列表的默認值。 我在下面解釋我的代碼。

<th>
<select style="width:100%; border:none; font-weight:bold;" ng-options="sub.name for sub in noCourse track by sub.value" ng-model="search" ng-change="selectedCourseTable();" >
<option value="">Course Name</option>
</select>
</th>

在這里,我正在動態獲取數據並將其綁定在select標簽中。上面的代碼生成以下html輸出。

<select style="width:100%; border:none; font-weight:bold;" ng-options="sub.name for sub in noCourse track by sub.value" ng-model="search" ng-change="selectedCourseTable();" class="ng-valid ng-touched ng-dirty ng-valid-parse">
<option value class  selected="selected">Course Name</option>
<option value="Master of computer application" label="Master of computer application">Master of computer application</option>
<option value="Bachelor of Technology" label="Bachelor of Technology">Bachelor of Technology</option>
<option value="Master in Technology" label="Master in Technology">Master in Technology</option>
</select>

subjectController.js:

$scope.selectedCourseTable=function(){
        if($scope.search.value=='Bachelor of Technology'){
            alert($scope.search.value);
        }
        if($scope.search.value=='Master in Technology'){
            alert($scope.search.value);
        }
        if($scope.search.value=='Master of computer application'){
            alert($scope.search.value);
        }
        if($scope.search==''){
            alert($scope.search.value);
        }
    }

在這里我無法檢查何時在ng-change事件中選擇文本作為Course Nameng-change選擇選項我可以使用此行$scope.search.value進行檢查,但是當用戶選擇此默認文本Course Name時無法檢查。請幫助我解決此問題。

刪除模板中的<option value="">Course Name</option>並在對象列表中添加相應的默認選擇將解決此問題。

無論您在哪里設置noCourse

$scope.noCourse = [{
    name :'Course Name',
    value: ''
}, /*rest of the values*/];
$scope.search = $scope.noCourse[0];

然后,您需要先修改該行,然后才能檢查對象:

// instead of this:
if ($scope.search == '') {
    alert($scope.search.value);
}

// do this
if ($scope.search.value == '') {
    alert($scope.search.value);
}

暫無
暫無

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

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