AngularJS如何从ng-options中选择值

时间:2014-10-29 05:33:35

标签: javascript c# angularjs

我在select ng-options中插入值,我想在特定月份的下拉列表中选择。 这是返回所有12个月的arrray,所以我怎么能从下拉列表中仅获取选定的月份。

脚本:

$scope.Month = ['Jan', 'Feb', 'March', 'April', 'May', 'June', 'July', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
 $scope.selectmonth = $scope.Month;

 $scope.SaveReport = function () {

    PrintReportRepository.save(
            {
             SelectMonth: $scope.selectmonth,
             },

HTML

 <select class="form-control" ng-model="selectmonth" ng-options ="months as months for months in Month">{{months}}</select>

2 个答案:

答案 0 :(得分:0)

要设置所选的第一项,请使用$scope.selectmonth = $scope.Month[0],设置$scope.selectmonth = $scope.Month U将整个数组设置为模型

答案 1 :(得分:0)

使用此代码:请参阅plunker

控制器:

$scope.Month = ['Jan', 'Feb', 'March', 'April', 'May', 'June', 'July', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
 $scope.SaveReport = function () {
    console.log('SelectMonth', $scope.selectmonth);
 }

HTML:

<select class="form-control" ng-model="selectmonth" ng-options ="months as months for months in Month" ng-change="SaveReport()"></select>