【问题标题】:dynamic ng-model for md-datepicker in ng-repeatng-repeat 中 md-datepicker 的动态 ng-model
【发布时间】:2017-09-26 00:16:08
【问题描述】:

我通过以下代码为出生日期动态添加 md-datepicker:

$scope.infData = [];
            $scope.addInf = function() {
            if ($scope.infData.length < 4) {
            $scope.infData.push({});
            }
        }; 

我的 html 如下所示:

<div class="col-xs-7 col-md-3"
ng-repeat="d in infData | limitTo: 4"
ng-value="d.value"
ng-class="{'md-align-top-left': $index==1}">
<md-input-container flex>
<label>infant D.O.B</label>
<md-datepicker
onkeydown="return false"
name="myDate{{$index}}"
ng-model="inf[$index==1].date"
md-hide-icons="all"
ng-required="true"
md-current-view="year"
md-min-date="ctrl.minDate"
md-max-date="ctrl.maxDate"
md-open-on-focus="true"
ng-messages>
</md-datepicker>

并且它们仅限于 4 。我的目标是获得如下日期的值,我为正常的日期选择器成功完成了:

if ($scope.inf.dateString = moment($scope.inf.date).format("YYYY-MM-DD")) {
    $scope.list.push(this.inf.dateString);

现在我的问题是: 1)我如何才能在 $index=1 时为这个日期选择器赋值(如果这个日期选择器没有推送,则不显示值)? 2)该值始终与今天一样最大日期(选择任何日期)? 3)我确定我的ng-model=inf[$index==1].date 是错误的,但我怎样才能让它变得动态呢? 4)提交$scope.inf = ""; &amp; $scope.inf = {};不工作后如何重置值并且值保持在那里

任何想法和帮助都会挽救我的一天。 附言: 我阅读了大部分关于 angularjs ng-model inside nested ng-repeat 的 stackover 问题,但没有一个对我有用

【问题讨论】:

    标签: javascript angularjs datepicker angular-material


    【解决方案1】:

    真的不明白这个问题,但也许你想要这个:

    $scope.addInf = function(index) {
        if ($scope.infData.length < 4) {
           $scope.infData.push({
             date: (index == 1 ? 'Some value you want' : '')
           });
        }
    }; 
    

    更改您的日期选择器 HTML

    ng-model="d.date"
    

    还有你的添加按钮 HTML

    <button ng-if="$last" ng-click="addInf($index)">myBtn</button>
    

    假设按钮在重复的div里面

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-30
      • 2013-07-10
      • 1970-01-01
      相关资源
      最近更新 更多