我没有使用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);
}
}
这里我无法检查我在Course Name
事件中选择ng-change
时的文字。其他选择选项我可以使用此行$scope.search.value
进行检查但是当用户选择此默认文字时Course Name
无法检查。请帮我解决此问题。
答案 0 :(得分:0)
删除模板中的<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);
}