2016-02-12 58 views
0

我想创建一个角度的月份的天数的下拉列表,所选的数字将被传递到elasticsearch查询中。
问题是,为了有一个'全天'选项查询要求所有输入为'*'。
代码工作,如果我也像这样的*显示的所有选项,但显然这不是对用户来说非常可读:角度选择选项不能正常工作

JS

$scope.daysInMonthArray = ['*'] 
$scope.day = $scope.daysInMonthArray[0]; 

$scope.daysInMonth = function (month, year) { 
    return 32 - new Date(year, month, 32).getDate() 
}; 

for (var i=0; i < $scope.daysInMonth(n, $scope.year); i++) { 
    $scope.daysInMonthArray.push(i + 1) 
} 

HTML

<select 
    class="form-control" 
    ng-model="day" 
    ng-options="day for day in daysInMonthArray"> 
</select> 

而且我有试图用下面的方法让它显示'all'这个单词,但它不起作用。它回发给查询的只是'[object object]'。

JS

$scope.daysInMonthArray = [{'name': 'all', 'value': '*'}] 
$scope.day = $scope.daysInMonthArray[0]; 

$scope.daysInMonth = function (month, year) { 
    return 32 - new Date(year, month, 32).getDate() 
}; 

for (var i=0; i < $scope.daysInMonth(n, $scope.year); i++) { 
    $scope.daysInMonthArray.push({'name': i + 1, 'value': i + 1}) 
} 

HTML

<select 
    class="form-control" 
    ng-model="day" 
    ng-options="day as day.name for day in daysInMonthArray track by day.value"> 
</select> 

我写的第二个错误还是有要去这个更好的办法?

+0

尝试使用该选项的索引,如果选择所有的传球被索引,然后解雇你的查询是指数。您也可以通过使用下拉列表的值来完成。 –

+0

您如何使用该值来查询elasticsearch。你应该只需要使用'day.value' –

+0

在发布我的答案之前,我没有看到你的第二个实现。看上去不错。我正在删除我的答案。 –

回答

0

我们可以将天数作为值并将标签对象传递给select。这样我们可以配置什么进入价值和什么进入标签。

app.js

var n = 1; 
    $scope.year = 2016; 

    $scope.daysInMonthArray = [{'value':'*', 'label':'all'}] 
    $scope.day = $scope.daysInMonthArray[0]; 

    $scope.daysInMonth = function (month, year) { 
    return 32 - new Date(year, month, 32).getDate() 
    } 

    for (var i=0; i < $scope.daysInMonth(n, $scope.year); i++) { 
    $scope.daysInMonthArray.push(
     { 
     'value':i + 1, 
     'label':i+1 
     }) 
    } 

    $scope.day = $scope.daysInMonthArray[0].value; 

因此你的选择将是

<select 
    class="form-control" 
    ng-model="day" 
    ng-options="day.value as day.label for day in daysInMonthArray"> 
</select>