2017-10-15 64 views
0

更新ngModel值在NG-UI引導日曆,我能夠正確地查看日期格式,但ngmodel裏面的值似乎是日期字符串像星期三2017年10月18日00:00 :00 GMT-0400(美國東部時間)。需要對角UI引導日期選擇器

我試圖更新我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,但也不會在這個代碼片斷工作,請 建議更新價值的最佳途徑。

+0

的[$ setViewValue](https://docs.angularjs.org/api/ng/type /ngModel.NgModelController#$setViewValue)方法自動調用一個摘要循環。你爲什麼覺得你需要使用'$ apply'? – georgeawg

+0

隨着[UIB-日期選擇器-彈出directve](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

+0

我正在嘗試更新ng-model值以覆蓋10/18/2017。 –

回答

0

添加該代碼在ng-change

$scope.searchScreenings = function(){ 
    var screeningDateFrom = $filter('date')($scope.searchInput.screeningDateFrom, 'mm/dd/yyyy'); 
} 

不要忘了在你的控制器注入$filter