잘 작동하는이 구성 요소가 있습니다. utc-> local 및 local-> utc 변환을 수행합니다 (응용 프로그램 논리에서 필요함).
모델에서 날짜는 항상 utc이지만 표시하려면 현지 시간으로 표시해야하며 UI가 변경되면 utc는 날짜로 변환됩니다.
$ scope. $ watch는 예상대로 작동합니다 - 사용자가 날짜를 변경하려고 할 때.
유일한 문제는 사용자가 날짜를 변경하지 않기로 결정한 경우이 $ scope입니다. $ watch 은 실행되지 않으며은 변환되지 않습니다.
이 상황에서 나는 무엇을 할 수 있습니까?
각도/타이프 라이터 구성 요소 :
class RsDateTimeController {
value: Date;
displayValue: Date;
static $inject = ["$scope"];
constructor($scope: ng.IScope) {
$scope.$watch(
"$ctrl.displayValue",
(newValue: Date, oldValue: Date) => {
if (newValue != oldValue) {
this.value = Utility.toUtcDate(newValue);
}
}
);
}
$onInit() {
if (this.value) {
this.displayValue = Utility.toLocalDate(this.value);
}
}
}
class RsDateTimeComponent implements ng.IComponentOptions {
public bindings: any;
public controller: any;
public template: string;
constructor() {
this.template = "<input type='datetime-local' class='form-control' ng-model='$ctrl.displayValue'>";
this.bindings = {
value: "="
};
this.controller = RsDateTimeController;
}
}
HTML : 정확히 $watch
의 기능입니다
app.component("rsDatetime", new RsDateTimeComponent());
<div class="form-group">
<label>@("Status Date".T())</label>
<rs-datetime ng-if="vm.woChangeStatus" value="vm.woChangeStatus.StatusDate"></rs-datetime>
</div>
각도 2.0을 사용하고 있습니까? 2.0에서는'$ watch'가 더 이상 필요하지 않기 때문에 묻습니다. – frishi
this.value를 기본값으로 초기화하지 않는 이유는 무엇입니까? 따라서 사용자가 날짜를 변경하지 않으면 기본값이 표시됩니다. – Hoyen
frishi, angular 1.5.хх – monstro