我不知道是否可以将.ts文件中的变量值绑定到输入值,最小和最大属性。 我已经尝试了很多方法,但是似乎都没有用。
我有两种方法可以从输入字段 [(ngModel)] =“ transferPoint.arrivalTime 中获取值,但是我需要将min和max属性设置为某些值。
这不起作用: min ='{{flight.departureTime}}'
这是我的html文件
<h1 mat-dialog-title>ADD NEW TRANSFER POINT</h1>
<div mat-dialog-content>
<mat-form-field>
<input type="datetime-local" id="arrival-time" name="arrival-time" value="{{flight.departureTime}}" min="{{flight.departureTime}}"
max="{{flight.arrivalTime}}" matInput [(ngModel)]="transferPoint.arrivalTime" placeholder="Arrival time">
</mat-form-field>
<br>
<mat-form-field>
<input type="datetime-local" id="departure-time" name="departure-time" value="2018-06-12T19:30" min="2018-06-07T00:00"
max="2018-06-14T00:00" matInput [(ngModel)]="transferPoint.departureTime" placeholder="Departure time">
</mat-form-field>
<br>
<mat-form-field>
<input matInput [(ngModel)]="transferPoint.countryAndCity" placeholder="Country and City">
</mat-form-field>
</div>
<div mat-dialog-actions>
<button class="submitbtn" mat-raised-button (click)='addTransferPoint()'>ADD</button>
<button class="cancelbtn" mat-raised-button mat-dialog-close>CANCEL</button>
</div>
这是我的.ts文件
export class AddTransferPointComponent implements OnInit {
errorMessage: string;
flightId: any;
flight: FlightDTO;
transferPoint: TransferPointDTO = new TransferPointDTO();
constructor(@Inject(MAT_DIALOG_DATA) public data: any,
public dialogRef: MatDialogRef<any>,
private transferPointService: TransferService,
private flightService: FlightService) { }
ngOnInit() {
this.flightId = this.data.flight;
}
getFlight() {
this.flightService.getFlight(this.flightId).subscribe(
data => {
this.flight = data;
}
)
}
addTransferPoint() {
this.transferPoint.flightId = this.flightId;
this.transferPointService.createTransferPoint(this.transferPoint).subscribe(
data => {
this.dialogRef.close();
location.reload();
},
error => {
console.log(error);
this.errorMessage = error.error.message;
}
);
}
}
答案 0 :(得分:1)
您可以使用属性绑定(另请参见https://angular.io/guide/template-syntax):
<input type="datetime-local" id="arrival-time" name="arrival-time" [value]="flight.departureTime" [min]="flight.departureTime" [max]="flight.arrivalTime" matInput [(ngModel)]="transferPoint.arrivalTime" placeholder="Arrival time">
答案 1 :(得分:0)
我在尝试执行 matInput type="datetime-local" 时遇到了类似的问题。当我调试它时,看起来 [min] 指令中的内容需要格式化为一个它可以理解的字符串......本质上以这种格式传递一个字符串:
YYYY-MM-DDTHH:MM
也许不是最好的,但在我的情况下,当我像这样切换数据时它可以工作
var month = this.formatDate(min.getMonth() + 1);
var day = this.formatDate(min.getDate());
this.minDate = min.getFullYear() + "-" + month + "-" + day + "T00:00";
格式日期是:
private formatDate(nmbr: number): string {
var date = nmbr + "";
date = (date.length < 2) ? "0" + date : date;
return date;
}
然后我可以在指令中使用它:
<input id="eventTime" matInput type="datetime-local" [min]="minDate" name="eventTime">