0
NG-UIのブートストラップカレンダーでは、日付の形式を正しく表示できますが、ngmodelの値はWed Oct 18 2017 00:00 :00 GMT-0400(EDT)。は、angle-uiのブートストラップdatepickerのngModel値を更新する必要があります
2011年10月18日の値を保持するようにngmodelを更新しようとしました。しかし、私はすでに$進行中のエラーに直面しています。
<input type="text" class="text-input" id="screeningDateFromSearch"
datepicker-popup uib-datepicker-popup="{{format}}"
placeholder="mm/dd/yyyy"
ng-model="searchInput.screeningDateFrom"
is-open="datesPicker.screeningDateFrom"
datepicker-options="dateOptions"
show-button-bar="false" ng-required="true" close-text="Close"
ng-change="searchScreenings()" />
angular.module('moduleName')
.directive('datepickerPopup', function (dateFilter) {
return {
require: '^ngModel',
restrict: 'EA',
link: function (scope, elm, attrs, ctrl) {
var dateFormat = attrs.uibDatepickerPopup;
attrs.$observe('datepickerPopup', function (newValue) {
if (dateFormat == newValue || !ctrl.$modelValue) return;
dateFormat = newValue;
ctrl.$modelValue = new Date(ctrl.$setViewValue);
});
ctrl.$formatters.unshift(function (modelValue) {
if (!dateFormat || !modelValue) return "";
var retVal = dateFilter(modelValue).format(dateFormat);
return retVal;
});
ctrl.$parsers.unshift(function (viewValue) {
var date = dateFilter(viewValue, dateFormat);
scope.$apply(function (viewValue) {
ctrl.$setViewValue(date);
ctrl.$render();
});
});
}
};
})
その後$ scope.safeApplyてみましたが、あまりにもこのコードスニペット に動作しないことは、値を更新するための最良の方法を提案してください。
[$ setViewValue](https://docs.angularjs.org/api/ng/typeを注入することを忘れないでください
ng-change
にこのコードを追加します。 /ngModel.NgModelController#$setViewValue)メソッドは自動的にダイジェストサイクルを呼び出します。なぜあなたは '$ apply'を使う必要があると思いますか? – georgeawg
[uib-datepicker-popup directive](https://angular-ui.github.io/bootstrap/#!#datepickerPopup)では、「ng-model」を「Dateオブジェクト」(https: //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date)文字列ではありません。 'datepicker-popup'ディレクティブは' uib-datepicker-popup'ディレクティブと戦っています。あなたはそれで何を達成しようとしていますか? – georgeawg
ng-modelの値を2017年10月18日に上書きするように更新しようとしています。 –