簡體   English   中英

帶有$ watchGroup的AngularJS datefilter

[英]AngularJS datefilter with $watchGroup

在2個壓延機實例的情況下,我需要為Angular UI-Calendar設置一些datetamp格式。 為了捕捉日期更改,我使用了watchGroup,因為兩個日歷需要相同類型的格式。 最后,我不能像對每個日歷使用兩個分開的$ watch那樣做:

        $scope.$watchGroup(['dt_start', 'dt_end'], function(mydate) {

        newdate = $filter('date')(mydate, "dd/MM/yyyy");
        console.log(newdate);
    });

輸出: [2015年1月2日星期五00:00:00 GMT + 0100(歐洲標准時間),...]

與分開的$ watch的:

 $scope.$watch('dt_start', function(mydate) {
        newdate = $filter('date')(mydate, "dd/MM/yyyy");
        console.log(newdate);
    });

    $scope.$watch('dt_end', function(mydate) {
        newdate = $filter('date')(mydate, "dd/MM/yyyy");
        console.log(newdate);
    });

我得到一個dd / MM / yyyy。 我在監視組表達式上做錯了什么?

$ watchGroup采用兩個數組和范圍作為參數

$scope.$watchGroup(['dt_start', 'dt_end'], function(newValues, oldValues, scope) {
    if (newValues[0] !== undefined) {
        var newStartDate = $filter('date')(newValues[0], "dd/MM/yyyy");
        console.log(newStartDate);
    }

    if (newValues[1] !== undefined) {
        var newEndDate = $filter('date')(newValues[1], "dd/MM/yyyy");
        console.log(newEndDate);
    }
});

您應該檢查任何undefined值。

AngularJS API

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM