【问题标题】:ng-option filter reservse select first valueng-option 过滤器保留选择第一个值
【发布时间】:2016-03-29 11:41:47
【问题描述】:

HI 尝试使用 filter/orderBy 选择第一个选项:但是它不起作用,我知道 |是角度的过滤函数。它目前选择最后一个值/标签,我该如何选择第一个标签(天)?

我尝试了 orderBy: -npu.label 和下面的代码,但没有运气。

vm.NoticePeriodUnitValue = [{
        value: 1,
        label: 'Days'
    }, {
        value: 7,
        label: 'Weeks'
    }, {
        value: 30,
        label: 'Months'
    }, {
        value: 365,
        label: 'Years'
    }];

<select name="NoticePeriodOptions" data-ng-change="vm.setNoticePeriod()" data-ng-model="vm.NoticePeriodUnit" 
            class="form-control search-input inverted mt-10" id="profile_notice-period-unit" 
            data-ng-options="npu.value as npu.label for npu in vm.NoticePeriodUnitValue | filter: -npu.label"> 
        </select>

【问题讨论】:

  • 你可以试试这个,过滤器:'-npu.label'
  • 我已经尝试过了,但它删除了其他选项并离开了月份。
  • 对不起,orderBy: '-npu.label'
  • 选择年份还是一样
  • 你想选择天数作为默认值

标签: angularjs


【解决方案1】:

将此添加到您的代码中,

vm.NoticePeriodUnit = vm.NoticePeriodUnitValue[0].value; 

这会给你一个默认值开始

【讨论】:

  • 然后使用 ng-option 中的 vm.NoticePeriodUni 来制作它(vm.NoticePeriodUnit 中的 npu),不,这给了我什么。
【解决方案2】:

您可以为模型设置默认值并删除该文件管理器。

$scope.vm = {
                NoticePeriodUnitValue: [{
                    value: 1,
                    label: 'Days'
                }, {
                    value: 7,
                    label: 'Weeks'
                }, {
                    value: 30,
                    label: 'Months'
                }, {
                    value: 365,
                    label: 'Years'
                }], 
               NoticePeriodUnit: 1
            }

【讨论】:

  • ng-options 是否仍然相同(.. 在 vm.NoticePeriodUnitValue 中)?
【解决方案3】:
Controller: 

    vm.NoticePeriodUnitValue = [{
            value: 1,
            label: 'Days'
        }, {
            value: 7,
            label: 'Weeks'
        }, {
            value: 30,
            label: 'Months'
        }, {
            value: 365,
            label: 'Years'
        }];
    vm.NoticePeriodUnit = {value: 1, label: 'Days'};
vm.setNoticePeriod = function() {
  console.log(vm.NoticePeriodUnit);
}

    HTML:
    <select name="NoticePeriodOptions" data-ng-change="vm.setNoticePeriod()" data-ng-model="vm.NoticePeriodUnit.value" 
                class="form-control search-input inverted mt-10" id="profile_notice-period-unit" 
                data-ng-options="npu.value as npu.label for npu in vm.NoticePeriodUnitValue"> 
            </select>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-13
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多