详解Angularjs在控制器(controller.js)中使用过滤器($filter)格式化日期/时间实例
Angularjs内置的过滤器(filter)为我们的数据信息格式化提供了比较强大的功能,比如:格式化时间,日期、格式化数字精度、语言本地化、格式化货币等等。但这些过滤器一般都是在VIEW中使用的,比如格式化时间/日期的VIEW视图代码:
<divng-app> <p> <label>Selectadate</label> <inputtype="date"id="date"ng-model="datevalue"/> </p> <p>{{datevalue|date:'fullDate'}}</p> </div>
那么问题来了,如果我需要在控制器(controller)的js代码中使用filter来格式化时间/日期,应该怎样处理呢?直接上代码吧:视图(view)模板代码:
<divng-app="dateApp"ng-controller="dateController"> <p>{{result}}</p> </div>
控制器(controller)代码:
varapp=angular.module('dateApp',[]); app.controller( 'dateController', function($scope,$filter){ $scope.result=$filter('date')(newDate(),'fullDate'); } );
以上就是在控制器(controller.js)的js代码中使用过滤器($filter)格式化日期/时间的实现。希望对大家的学习有所帮助,也希望大家多多支持毛票票。