我有下拉列表:
<select name="timefilter" id="timefilter">
<option value="Last week">@Translate("LAST_WEEK")</option>
<option value="Last 3 days">@Translate("LAST_3_DAYS")</option>
<option value="Yesterday">@Translate("YESTERDAY")</option>
<option value="Today">Translate("TODAY")</option>
</select>
我需要将值从下拉列表发送到$scope
$scope.GetMenuForSelectedTime = function (numberOfDays) {
}
我该怎么做?
编辑:我需要打电话的功能:
$scope.SelectTicketByTime = function (range) {
var range = range;
var date = new Date();
var day = date.getUTCDate().padLeft();
var monthIndex = (date.getUTCMonth() + 1).padLeft();
var year = date.getUTCFullYear();
var hours = date.getUTCHours().padLeft();
var minutes = date.getUTCMinutes().padLeft();
$scope.GetMenuForSelectedTime = function (numberOfDays) {
var d = new Date();
d.toUTCString();
day = date.getUTCDate() - numberOfDays;
day = day.padLeft();
var offset = d.getTimezoneOffset();
d.setHours(00);
var hoursToAdd = offset / 60;
d.setHours(hoursToAdd);
if (day > daysInMonth(monthIndex, year)) {
var dayOffset = day - daysInMonth(monthIndex, year);
day = dayOffset.padLeft();
monthIndex = (date.getUTCMonth() + 2).padLeft();
}
var To = year + monthIndex + day + d.getHours() + '00';
$scope.to = To;
$http.get(TicketUrl + 'GetTickets/0/0?DateFrom=' + $scope.from + '&DateTo=' + $scope.to).
then(RecivedTickets, ErrorResponse);
}
if (range == "Today") {
$scope.GetMenuForSelectedTime(0);
}
else if (range == "Yesterday") {
$scope.GetMenuForSelectedTime(1);
}
else if (width == "Last 3 days") {
$scope.GetMenuForSelectedTime(3);
} else if (width == "Last 7 days") {
$scope.GetMenuForSelectedTime(7);
}
答案 0 :(得分:2)
这是putvande回应的例子(感谢他):
<select name="timefilter" id="timefilter" ng-model="selectedTime">
<option value="Last week">@Translate("LAST_WEEK")</option>
<option value="Last 3 days">@Translate("LAST_3_DAYS")</option>
<option value="Yesterday">@Translate("YESTERDAY")</option>
<option value="Today">Translate("TODAY")</option>
</select>
然后您将在控制器中获得选项的值:
$scope.selectedTime //The selected option
编辑:控制器中的函数在param中不需要值,因为控制器已经知道这个值。你应该有这样的东西。
$scope.SelectTicketByTime = function () {
var range = $scope.selectedTime;
var date = new Date();
var day = date.getUTCDate().padLeft();
var monthIndex = (date.getUTCMonth() + 1).padLeft();
var year = date.getUTCFullYear();
var hours = date.getUTCHours().padLeft();
var minutes = date.getUTCMinutes().padLeft();
}
答案 1 :(得分:0)
我使用ng-options
修改代码,当您更改下拉列表中的任何选项时,ng-change
将会触发
<select name="ruleDetails" ng-model="selectedTime"
ng-options="time.id as time.value for time in timeDetails"
ng-change="GetMenuForSelectedTime(selectedTime)">
<option value="">-Select Time-</option>
</select>
$scope.GetMenuForSelectedTime = function(id) {
alert(id);
};
<强>更新强>
此代码也有效,value
将传递给函数。
<select name="timefilter" id="timefilter"
ng-model="selectedTime"
ng-change="GetMenuForSelectedTime(selectedTime)">
<option value="Last week">@Translate("LAST_WEEK")</option>
<option value="Last 3 days">@Translate("LAST_3_DAYS")</option>
<option value="Yesterday">@Translate("YESTERDAY")</option>
<option value="Today">Translate("TODAY")</option>
</select>
请检查此plunker