【问题标题】:Angular-strap datepicker doesn't update existing model valueAngular-strap datepicker 不会更新现有模型值
【发布时间】:2014-02-12 22:52:57
【问题描述】:

我正在为我的 angularjs SPA 项目使用来自 http://mgcrea.github.io/angular-strap/##datepickers 的 angular-strap datepicker。使用 ngModel 双向绑定编辑现有记录时,Angular 不会检测到日期字段更改,即使我通过更新另一个非日期字段来强制保存表单,新日期也不会在后端更新。这是我的html文件中的相关部分:

<input name="DecisionDatePicker" id="ddpID" type="text" class="form-control input-medium" tabindex="14" placeholder="{{vm.format}}"
   data-date-format="MM-dd-yyyy" 
   data-ng-model="vm.formData.dateDecision"
   data-ng-required="vm.decisionDateRequired"
   bs-datepicker />

我没有在我的 js 文件中做任何特别的事情。我正在使用breathjs 来处理我的数据,它在其他领域工作得很好。 我究竟做错了什么?任何帮助表示赞赏。

【问题讨论】:

  • 我正在努力解决类似的问题(Breeze 实体已从 timepicker 更新,但未调用 entityChanged 事件)。你有没有找到解决这个问题的方法?
  • 我也遇到了这个问题。
  • 我也有同样的问题

标签: angularjs datepicker breeze angular-strap


【解决方案1】:

我也遇到过同样的问题,我用一个自定义指令解决了这个问题,该指令在 bs-datepicker 调度模糊事件时更新模型:

angular.module('moduleName').directive('updateModelOnBlur', 
      ['$parse', function($parse) {
        return {
          restrict: 'A',
          require: 'ngModel',
          link: function (scope, elm, attrs) {
              elm.bind("blur", function (event) {
                scope.$apply(function () {
                  var model = $parse(attrs.ngModel);
                  model.assign( scope, elm.context.value );
                });
              });
          }
        };
      }]
    );

我知道这只是一个技巧,而不是问题的最终解决方案。但是,如果您遇到困难并想继续前进,那么有时 hack 可能会很有用。

如果您想在更新模型之前操作日期格式,您还可以在指令中包含 $filter 服务。 ;)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-05
    • 1970-01-01
    相关资源
    最近更新 更多