2017-10-15 66 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