2014-03-05 26 views
3

我有以下控制器在我angularjs控制器如何angularjs控制器使用轉換器對象的值

.controller('taskCtrl', ['$scope', function ($scope) { 
    $scope.getRef = function() { 
     return getTask(); 
    }; 

    $scope.save = function() { 
      $.extend(true, getTask(), $scope.data); 
    }; 
}]) 

和我的模板:

<tr> 
    <td> 
    <label class="ngTemplateTitle" > create Date</label> 
    </td> 
    <td> 
    <input class="form-control" id="data.creationDate" type="text"ng-model="data.creationDate"/> 
    </td> 
</tr> 

我需要轉換和查看陽曆日期的形式回曆。 然後用戶修改後的對象的值將hijri轉換爲公曆日期。

回答

1

您是否熟悉moment.js

你可以把它導入到你的項目(很輕的文件),並實施角度濾波器用於解析它,你想要的方式...

類似:

angular.module('myFilters').filter('formatDateExample', function() { 
var defaultFormat = "l"; 
return function(timestamp, format) { 
    format = format || defaultFormat; 
    return timestamp ? moment(new Date(timestamp)).format(format) : null; 
    // when you'll call the filter from the HTML, it will parse it to gregorian date 
}; 

});

在HTML模板

<div>{{myDateVar | formatDateExample }}</div> 

或直接從HTML自定義格式:

<div>{{myDateVar | formatDateExample: "DD/MM/YYYY, HH:MM" }}</div> 

的想法是保持你的約會對象以相同的方式在模型中保存只有當你想在你的視圖中顯示它時,使用過濾器解析

相關問題