简体   繁体   English

角材料DateTimepicker

[英]Angular Material DateTimepicker

I am using this http://logbon72.github.io/angular-material-datetimepicker/ (Angular Material Date Time picker) It works well when only one is present 我正在使用此http://logbon72.github.io/angular-material-datetimepicker/ (角度材料日期时间选择器),当只有一个时,它会很好地工作

<md-input-container flex-gt-md="200">
    <input time="false" date="true" mdc-datetime-picker type="text" id="date"
           placeholder="Posting Date" format="YYYY-MM-DD"
           ng-model="posting_date" min-date="minDate" max-date="maxDate">
</md-input-container>

But it fails when I code like this in (ng-repeat) 但是当我在(ng-repeat)中这样编码时失败了

<div ng-repeat="JobExp in jobpostings">
    <md-input-container flex-gt-md="200">
        <input time="false" date="true" mdc-datetime-picker type="text" id="date"
               placeholder="Posting Date" format="YYYY-MM-DD" md-select="changed()"
               ng-model="JobExp.posting_date" min-date="minDate" max-date="maxDate">
    </md-input-container>
</div>

I tried calling md-select function but it doesnt call nor ng-select I tried using $watch like this 我尝试调用md-select函数,但是它不调用也不用我使用$ watch进行ng-select这样的操作

$scope.$watch("JobExp.posting_date", function(newValue, oldValue) {
    $scope.JobExp.posting_date = $filter('date')(new Date($scope.JobExp.posting_date), 'yyyy-MM-dd');
    console.log("PostinDate:"+$scope.JobExp.posting_date);

}); 

It says cannot read the property of undefined near posting_date 它说无法在posting_date附近读取未定义的属性

What mistake I am doing ? 我在做什么错? How to achieve this ? 如何实现呢?

One problem which I see is that, in your ng-repeat , for all the date-picker, the ng-model is JobExp.posting_date . 我看到的一个问题是,在您的ng-repeat ,对于所有的日期选择器, ng-modelJobExp.posting_date Which is logically wrong because all your datepickers will be binded to one scope variable and changing one will change the value in all the others. 从逻辑上讲这是错误的,因为您所有的日期选择器都将绑定到一个范围变量,而更改一个范围变量将更改所有其他范围值。

You should consider using separate ng-model for each datepicker. 您应该考虑为每个日期选择器使用单独的ng-model You can have separate ng-model like this: 您可以这样设置单独的ng-model

<input time="false" date="true" mdc-datetime-picker type="text" id="date"
           placeholder="Posting Date" format="YYYY-MM-DD" md-select="changed()"
           ng-model="posting_date{{$index}}" min-date="minDate" max-date="maxDate">

This will give you separate ng-model for each datepicker like this: posting_date0 , posting_date1 and so on... 这将为每个日期选择器提供单独的ng-model ,如下所示: posting_date0posting_date1等...

In order to put a watch on all the models, you can create a watch on collection like this: 为了在所有模型上放置手表,您可以像这样在集合上创建手表:

$scope.$watchCollection('[posting_date0, posting_date1]', function(newValues, oldValues){
  // do stuff here
  // newValues and oldValues contain the new and respectively old value
  // of the observed collection array
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM