Bootstrap-ui datepicker:如何拥有一个空的ng模型?

时间:2016-02-01 15:41:48

标签: angularjs datepicker angular-ui-bootstrap isnullorempty

在一个角度项目中,我有一个bootstrap-ui datepicker,用于提供付款发票日期的信息。

最初,在创建发票时,由于尚未支付,因此我应该有一个ng-model变量指示null或类似的东西:

$scope.invoice.Payment_Date = null

问题是,似乎datepicker需要有一个有效的日期对象才能工作。当以这种方式使用所有内容时,当我在日期选择器中选择日期以说明我的发票已付款时,日期选择器指示正确的日期,如2016年2月15日星期二,但ng-model变量保持为空。

如果我这样初始化变量:

$scope.invoice.Payment_Date = new Date();

然后在更改datepicker日期时,ng-model会按原样更新。

问题是,我可能必须创建/更新发票而不说它已经付款,这样做,我得到一个Payment_Date,它具有发票创建/更新日期的值。

有没有办法让这个变量设置为null或者当datepicker保持不变时为空,并且在使用它时获得正确的datepicker值?

希望有人可以提供帮助!

编辑:这里是datepicker的HTML代码:

<form name="formFacture" novalidate rc-submit="modifierFacture()">  
<div class="form-group">
    <label>Date de paiement : </label>
        <p class="input-group">
            <input type="text" class="form-control" uib-datepicker-popup="dd MMMM yyyy" ng-model="facture.Date_Paiement" is-open="popupDatePaiement.opened" datepicker-options="dateOptions" date-disabled="disabled(date, mode)" close-text="Fermer" clear-text="Effacer" current-text="Aujourd'hui" readonly="readonly" ng-click="openDatePaiement()" />
            <span class="input-group-btn">
                <button type="button" class="btn btn-default" ng-click="openDatePaiement()"><i class="glyphicon glyphicon-calendar"></i></button>
            </span>
        </p>
    </div>
<div class="container text-center">
    <button type="submit" class="btn btn-primary">Enregistrer</button>
    <div class="btn btn-primary" ng-click="ChangeLocation('prev', '/#factures')">Retour</div>
</div>

</form>

这是JS代码:

$scope.facture = {};
$scope.dateOptions = {
   startingDay: 1
 };
$scope.popupDatePaiement = {
   opened: false
};
var openDatePaiement = $scope.openDatePaiement = function() {
   $scope.popupDatePaiement.opened = true;
};

$scope.modifierFacture = function(){
   console.log($scope.facture);
   return
}

在查看代码后,我发现:

  1. 我可以使用datepicker获得一个有效的日期(ng-model出现轻微错误)但如果我更改日期两次,则facture.Date_Paiement变量保留第一个选项,第二个选项不会被转发它。
  2. 如果我使用&#34;清除&#34; datepicker内的按钮,facture.Date_Paiement变量不会恢复为null
  3. 我已尝试添加此行:

    $ scope.Date_Paiement = null

  4. 在openDatePaiement函数中,但在这种情况下,facture.Date_Paiement始终为null。

1 个答案:

答案 0 :(得分:3)

好的,花了几个小时研究这个问题的原因,我找到了答案。

不要依赖console.log(对象)来告诉你对象内部是什么,因为看起来解析对象以在控制台内呈现它的函数会被窃听!

相反,使用console.log(JSON.stringify(object))并且您将拥有正确的值。

我的脑袋仍然疼痛,当这些事情被窃听时我们该怎么办?