如何使用ng-options以角度为单位获取所选选项的文本

时间:2013-07-07 11:35:07

标签: angularjs ng-options

我的代码位于jsFiddle,以下是该代码的c / p。我的问题是,当我点击 Monday 选项时,如何获取 Mon 而不是 [“Mon”] ,以及如何得到周一,周四当我多选周一和周四(ofc,你得到点:星期二,星期三,星期六,当选择星期二,星期三和星期六选项时)。我必须遗漏一些明显的东西,所以请引导我朝正确的方向前进。

HTML:

 <div ng-app ng-controller="Controller">
    <select multiple="multiple" ng-model="day" ng-options='day.id as day.name for day in days'></select>

    {{day}}
</div>

JS:

function Controller($scope) {
    $scope.days = [
        {
            name: "Monday",
            id: "Mon"
        },
        {
            name: "Tuesday",
            id: "Tue"
        },
        {
            name: "Wednesday",
            id: "Wed"
        },
        {
            name: "Thursday",
            id: "Thu"
        },
        {
            name: "Friday",
            id: "Fri"
        },
        {
            name: "Saturday",
            id: "Sat"
        },
        {
            name: "Sunday",
            id: "Sun"
        }
    ];    
}

1 个答案:

答案 0 :(得分:1)

这是an example,它将数组转换为String并直接在页面中将其显示为字符串:

JS:

$scope.selectedDaysToString = function() {
    return $scope.day.join(', ');
}

HTML:

{{selectedDaysToString()}}

此处an example生成一个包含所有选定日期的列表:

HTML:

<ul>
    <li ng-repeat="d in day">{{d}}</li>
</ul>