【问题标题】:Angular Moment Picker don't overwrite ng-model valueAngular Moment Picker 不会覆盖 ng-model 值
【发布时间】:2017-01-11 01:11:46
【问题描述】:

我在我的项目中使用Angular Moment Picker。在编辑页面时,ng-model 的值应该被 API 中的数据覆盖,但是这些值不会覆盖到时刻选择器 ng-model,而不是将 ng-model 值与 undefined 绑定。如果我删除时刻选择器它确实有效。下面是我的情况的演示。

JSFiddle

查看

 <input name="time_of_time"
    class="form-control"
    placeholder="Select a time"
    ng-model="scheduler.timeOfTime"
    ng-model-options="{updateOn: 'blur'}"
    moment-picker="scheduler.timeOfTime" //Working if remove this line
    format="HH:mm">

控制器

myApp.controller('HomeCtrl', function ($scope) {
    $scope.scheduler = {};

    $scope.scheduler.timeOfTime = '11:10';

    console.log($scope.scheduler);

【问题讨论】:

    标签: angularjs momentjs angular-moment


    【解决方案1】:

    我遇到了同样的问题。

    这条评论对我有帮助。 https://github.com/indrimuska/angular-moment-picker/issues/92#issuecomment-263669991

    来自链接。有两种方法可以做到这一点。 1.使用来自moment-picker属性的格式化字符串日期

    <input moment-picker="ctrl.formattedDate"
           ng-model="ctrl.momentDate"
           ng-model-options="{ updateOn: 'blur' }"
           format="DD MMM YYYY"
           start-view="month">
    
    <!-- Use `ctrl.formattedDate` anywhere -->
    Formatted date: {{ ctrl.formattedDate }}
    

    2。使用 Moment.js 对象检索格式化的日期 从矩选择器中删除参数

    <input moment-picker
           ng-model="ctrl.momentDate"
           ng-model-options="{ updateOn: 'blur' }"
           format="DD MMM YYYY"
           start-view="month"
           change="ctrl.setFormattedDate(newValue)">
    
    <!-- Use `ctrl.formattedDate` anywhere -->
    Formatted date: {{ ctrl.formattedDate }}
    

    在你的控制器中

    ctrl.setFormattedDate = function (momentDate) {
       // do you stuff.. and
       ctrl.formattedDate = momentDate.format('DD MMM YYYY');
    };
    

    我最终选择了方法 2。 希望对您有所帮助。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多