【问题标题】:Angular UI bootstrap datepickerAngular UI 引导日期选择器
【发布时间】:2016-01-07 12:24:59
【问题描述】:

我正在使用 Angular UI bootstrap 的 datepicker ,当我以编程方式更改日期时,视图不会改变,当我在 11 月 30 日并且我以编程方式将日期更改为 12 月 2 日时,日期更改但视图在 11 月仍然固定,这是我编写下一个日期按钮的方法。

$scope.right=function(){
    if($scope.viewmodel.timeResolution=='yearly')
        $scope.dt.setFullYear($scope.dt.getFullYear()+1);
    else if($scope.viewmodel.timeResolution=='monthly')
        $scope.dt.setMonth($scope.dt.getMonth()+1);
    else if($scope.viewmodel.timeResolution=='daily') {
        $scope.dt.setDate($scope.dt.getDate() + 1);
    }
    $scope.changedValue();
};

HTML:

 <uib-datepicker ng-model="dt" id="dp" datepicker-mode="mode" init-date="initDate" show-weeks=false max-mode="maxMode"  min-date="minDate" max-date="maxDate" min-mode="minMode" class="well well-sm"  ng-change="changedValue(viewmodel.dt)"></uib-datepicker>

如何在每次在日期选择器上以编程方式更改日期时模拟刷新视图?

【问题讨论】:

  • 可以添加datepicker的html代码吗?
  • @azelix 是否有添加日期的业务规则?你想禁用周末吗?
  • @JoaozitoPolo 我添加了它

标签: javascript angularjs twitter-bootstrap datepicker


【解决方案1】:

您需要再次转换为日期。函数集...只返回时间戳:

$scope.right=function(){
    if($scope.viewmodel.timeResolution=='yearly')
        $scope.dt = new Date($scope.dt.setFullYear($scope.dt.getFullYear()+1));
    else if($scope.viewmodel.timeResolution=='monthly')
        $scope.dt = new Date($scope.dt.setMonth($scope.dt.getMonth()+1));
    else if($scope.viewmodel.timeResolution=='daily') {
        $scope.dt = new Date($scope.dt.setDate($scope.dt.getDate() + 1));
    }
    $scope.changedValue();
};

【讨论】:

  • 我不知道您的 changedValue() 函数,但 datepicker 不需要任何其他调用即可工作。
  • 你救了我在这里,你的答案是正确的,现在它工作得很好,并且对于 changedValue() 它确实生成地图和时间序列,我的意思是它与 datepicker 没有任何关系本身。
  • 不客气。使用该组件时,我也遇到了一些时区问题,但它可能会根据您的后端实现而有所不同。
【解决方案2】:

为了让您对角度日期的整体体验更好,您应该查看 moment .js

http://momentjs.com/

它允许您轻松创建、格式化、添加和减去日期,并且问题最少。使用默认的 date.set 函数可能会导致很多问题。

我有一个这样的设置,使用没有问题的时刻

【讨论】:

  • 这可能是我稍后会研究的问题,但这不是我现在遇到的问题,日期更改为我想要的日期没有问题,但我的观点datepicker 不遵循日期,例如,月份从 11 月更改为 12 月
猜你喜欢
  • 2015-06-11
  • 2016-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多