2017-10-19 224 views
0

我試圖從下拉列表中選擇與angularjs的值。當我選擇的價值我得到這個錯誤angular.min.js:108 TypeError: Cannot read property 'ftype' of undefined從angulars下拉列表中獲取選擇值讀取錯誤

HTML

<select name="select" ng-model="queryBy" ng-change="get_search_value(item)"> 
    <option ng-repeat="item in filter" value="{{item.ftype}}">{{item.ftype}}</option> 
</select> 

JS

$scope.search_type=''; 
    $scope.filter = [  
    { id: 1, ftype: "Complaint Type"}, 
    { id: 2, ftype: "Region" }, 
    { id: 3, ftype: "District"}, 
]; 

    $scope.get_search_value=function(item){ 
$scope.search_type=item.ftype; 
alert(item.ftype) 

    } 

回答

0

我想你誤解了AngularJS ......它所有的這個要求。您不需要更新單獨的值,也不需要更改事件就可以爲您執行此操作。

這是我想你想實現:

HTML

<select name="select" ng-model="queryBy"> 
    <option ng-repeat="item in filter" value="{{item.ftype}}">{{item.ftype}}</option> 
</select> 

的JavaScript

$scope.filter = [  
    { id: 1, ftype: "Complaint Type"}, 
    { id: 2, ftype: "Region" }, 
    { id: 3, ftype: "District"} 
]; 

// you can access the selected value by using $scope.queryBy 

這是正確的,只是使用$scope.queryBy(或只是queryBy在你的HTML中)會給你所選的值選項。正如tymeJV所提到的,您也可以使用ngOptions而不是ngRepeat作爲選項。我也建議您考慮使用ngBind而不是大括號,這樣可以避免在評估之前表達本身閃爍。

+0

但功能在哪裏 – user6579134

+0

這是不需要的,AngularJS會自動更新'$ scope.queryBy'以匹配選定的選項的值。如果您需要在變更時做其他事情,則可以將其添加回來,但不需要更新搜索類型。 –

1

我的猜測是item是對<select>水平不可見。無論在<option>(所以,「項目」)中的任何值都應該保存在ng-model值中,即queryBy。你爲什麼將item傳遞給get_search_value?嘗試在get_search_value功能acessing的queryBy代替

0

您應該使用ngOptions - 你item無法訪問您目前擁有的方式。這裏有一個快速重構,應該工作:

相關問題