2017-01-15 34 views
1

我試圖添加默認值到NG-選項,但我不能這樣做的下拉列表中選擇默認值。 這裏是我的代碼 我不能夠選擇使用角度濾波

var app = angular.module('jobs',[]).constant('API_URL', 'http://localhost:8000/'); 

app.controller('jobsController', function($scope, $http, API_URL, $timeout){ 
    $scope.selectedItem = "Years"; 
    console.log("hihi"); 
}); 

angular.module('jobs').filter("minmax", function(){ 
    return function(arr, min, max){ 
     min = parseInt(min); 
     max = parseInt(max); 
     for(var i =min; i <= max; i++){ 
      arr.push(i); 
     } 
     return arr; 
    }; 
}); 


<div class="m-banner-1" ng-controller="jobsController"> 
    <select class="ui fluid normal dropdown" data-ng-model="selectedItem" 
      ng-options="opt as opt for opt in [] | minmax:0:30"> 
    </select> 
</div> 

這裏的a link

我要選擇默認的「年」

請幫

+0

這是沒有意義的。您的選項數組包含數字。 「年」不是一個數字,也不是任何選項,所以不能選擇。即使這樣,如果你想$ scope.selectedItem是選擇,NG-模式應該是'NG-模型=「將selectedItem」'。不是'ng-model =「proofGroupId」'。你究竟想要什麼?你想要一個顯示數字的選擇框,在頂部顯示「年」的默認選項? –

+0

是正是我想要顯示數字的選擇框,並在頂部顯示「年」 –

回答

0

正如我在評論中說,您的選擇陣列包含數字。 「年」不是一個數字,也不是任何選項,所以不能選擇。即使這樣,如果你想$scope.selectedItem是選擇,NG-模式應該ng-model="selectedItem"。不是ng-model="proofGroupId"

要顯示「默認選項」,the correct way to do它是在HTML中添加一個沒有值的選項和所需的標籤。

使用過濾器,在每個消化再現和重新填充一個新的數組也不是一個好主意,在所有的,只是使事情變得更加複雜和更高效的比他們應。這不是過濾器的用途。

下面是它看起來應該像:

var app = angular.module('jobs',[]); 

app.controller('jobsController', function($scope) { 
    $scope.opts = []; 
    for (var i = 0; i <= 30; i++) { 
    $scope.opts.push(i); 
    } 
}); 

<div class="m-banner-1" ng-controller="jobsController"> 

    <select class="ui fluid normal dropdown" ng-model="proofGroupId" 
      ng-options="opt as opt for opt in opts"> 
    <option value="">Years</option> 
    </select> 

    {{ proofGroupId }} 
</div> 

這裏,它是in a plunkr

+0

謝謝你的回答,現在suggestion.This工作正常默認選項。 –

0

您需要手動添加空的默認選項:

<select class="ui fluid normal dropdown" 
    ng-model="selectedItem" 
    ng-options="opt as opt for opt in [] | minmax:0:30"> 
    <option value="">Years</option> 
</select>