【问题标题】:AngularJS - ng-repeat default value for select optionAngularJS - 选择选项的 ng-repeat 默认值
【发布时间】:2016-12-11 08:15:37
【问题描述】:

我有一个表单,用户需要在其中选择两周的轮班类型。我需要做的是为每个单元格设置 UNIQUE 默认值,例如Day 1: D, Day2:N, etc.。我怎样才能做到这一点?

小提琴:https://jsfiddle.net/fujik8467/axnm0cy9/24/

【问题讨论】:

    标签: angularjs select ng-repeat


    【解决方案1】:

    您可以使用此代码:

     <select ng-model="day.selectedShiftType" ng-options="shift as shift.strId for shift in shifts" ng-init="day.selectedShiftType = shifts[0]">
    

    上面将始终选择第一个选项,但我们可以使用以下代码使其随机: $scope.GetRandomIndex = function() { return Math.floor((Math.random()*3)+1); }

    html代码:

    <select ng-model="day.selectedShiftType" ng-options="shift as shift.strId for shift in shifts" ng-init="day.selectedShiftType = shifts[GetRandomIndex()]">
    

    【讨论】:

    • 以上将始终选择第一个选项,但我们可以使用此代码:$scope.GetRandomIndex = function(){ return Math.floor((Math.random()*3)+1); }, 和 html 代码:&lt;select ng-model="day.selectedShiftType" ng-options="shift as shift.strId for shift in shifts" ng-init="day.selectedShiftType = shifts[GetRandomIndex()]"&gt;
    • 如果我想从我的 controller.js 中特意设置特定选项怎么办?例如,我有一组现有的选择(Day1,'D'...),我想将它们加载为默认值?
    • 您可以通过发送天数作为参数来修改方法,并相应地添加if条件。您有订单还是要随机选择?
    • 我很早就回复了,让我去看看。
    • console.log 揭示了我需要什么以及我需要解决的职位,但它仍然没有选择我指定的默认项目
    猜你喜欢
    • 2014-02-16
    • 2014-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-28
    • 1970-01-01
    相关资源
    最近更新 更多