2016-01-21 17 views
0

我具有其中用戶可以選擇一個ORDERBY濾波器一個選擇元件,NG-模型不是通過正確的值

%select{ 
    "ng-change" => "select()", 
    "ng-model" => "selectedItem", 
    "ng-options" => "option.sortBy for option in listOfOptions"} 

這是listOfOptions

$scope.listOfOptions = [ 
    {sortBy: 'Release date', value:'release_date'}, 
    {sortBy: 'Newly added', value:'created_at'} 
]; 

select功能,

$scope.select = function(){ 
    console.log($scope.selectedItem.value) 
} 

在視圖中,選擇框顯示發佈日期和新添加的選項,但是當我選擇其中之一,我得到的錯誤,

TypeError: Cannot read property 'value' of undefined

所以它看起來像$ scope.selectedItem是不確定的,但我想不出爲什麼。

+0

如果我沒有弄錯selectedItem應該出現在這個不在$ scope對象中。 * this.selectedItem *應該爲你工作 –

+0

你應該將ng-options改爲低於'「ng-options」=>「在listOfOptions中選擇」}' –

+0

@從範圍改變到這個固定它。 – alucardu

回答

0

嗯,我做了一個plunker和選擇按預期工作

Plunker

app.js:

$scope.listOfOptions = [{ 
    sortBy: 'Release date', 
    value: 'release_date' 
    }, { 
    sortBy: 'Newly added', 
    value: 'created_at' 
    }]; 

    $scope.select = function() { 
    console.log($scope.selectedItem.value) 
    } 

的Index.html:

<select ng-change="select()" ng-options="option.sortBy for option in listOfOptions" ng-model="selectedItem"></select> 
<p>{{selectedItem}}</p> 

你能提供您角度版本?

相關問題