ng-option filter reservse选择第一个值

时间:2016-03-29 11:41:48

标签: angularjs

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>

3 个答案:

答案 0 :(得分:1)

将此内容添加到您的代码中

rewrites: [
  {
    from: /^\/search-result/,
    to: 'index.html'
    }
  }
]

将为您提供以

开头的默认值

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

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>