我如何在AngularJS查看格式的数据? [英] How do I format data in a view in AngularJS?

查看:183
本文介绍了我如何在AngularJS查看格式的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<输入类型=文本NG模型=user.User.DateOfBirth> 是我的观点。 user.User.DateOfBirth 目前在年月日格式,但我想让它显示为在视图MM / DD / YYYY 格式。是否有可能转换这个只是为了显示?

<input type="text" ng-model="user.User.DateOfBirth"> is in my view. user.User.DateOfBirth is currently in YYYYMMDD format, but I want it to show as mm/dd/yyyy format in the view. Is it possible to convert this just for the display?

推荐答案

有关以正确的格式输出数据尽量过滤器(在你的案件日期过滤器:的 http://docs.angularjs.org/api/ng.filter:date

For outputting the data in the correct format try filters (in your case the date filter: http://docs.angularjs.org/api/ng.filter:date)

那么,对于输入,是另一种情况。我想你可以总是输入绑定到另一个属性,看是和自己的分析,然后将其分配给您真正的模型......

Well, for inputs that's another case. I guess you could always bind that input to another property, watch that and parse it yourself and then assign it to your real model...

更深入的了解,你可以尝试这样的事:

Some more insight, you could try something like this:

HTML

<input type="text" ng-model="myDateInput">

在您的JS控制器使用如下:

In your JS controller use the following:

$scope.$watch('myDateInput', function (newValue) {
    $scope.user.User.DateOfBirth = $filter('date')(newValue, 'yyyy-MM-dd'); // Or whatever format your real model should use
});

$scope.$watch('user.User.DateOfBirth', function (newValue) {
    $scope.myDateInput = $filter('date')(newValue, 'yyyy-MM-dd'); // Or whatever format your input should use
});

有可能会更好虽然强制用户使用特定的输入格式,或者通过某种模式匹配(因此验证)或通过某种特殊日期输入/小窗口的

It would probably be better though to force the user to use a certain input format, either by some sort of pattern matching (and therefore validation) or by some sort of special date input/widget.

这篇关于我如何在AngularJS查看格式的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆