在输入元素使用angularjs过滤器 [英] Using angularjs filter in input element

查看:213
本文介绍了在输入元素使用angularjs过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我没有错过在DOCO什么明显的,如果我有我相信总有人会有所帮助。

I hope I haven't missed anything obvious in the doco, if I have I'm sure someone will help.

我使用asp.net的WebAPI返回DTO,与日期字段。这些是使用JSON.Net序列化(格式为2013-03-11T12:37:38.693')。

I'm using asp.net webapi to return a DTO, with date fields. These are serialized using JSON.Net (in format '2013-03-11T12:37:38.693').

我想使用的过滤器,但在输入元素,这是可能的,或者我应该创建一个新的过滤器或指令来完成呢?

I'd like to use a filter but in an INPUT element, is this possible or should I create a new filter or directive to accomplish this?

// this just displays the text value
<input ui-datetime type="text" data-ng-model="entity.date" /> 
// this doesn't work at all
<input ui-datetime type="text" data-ng-model="{{entity.date|date:'dd/MM/yyyy HH:mm:ss a'}}" /> 
// this works fine
{{entity.date|date:'dd/MM/yyyy HH:mm:ss a'}}

有没有我缺少什么捷径?

Is there any shortcut I'm missing?

推荐答案

在简而言之:如果你希望你的数据有不同的重新presentation在视图和模型中,您将需要一个指令,你可以认为的双向过滤器

In short: if you want your data to have a different representation in the view and in the model, you will need a directive, which you can think of as a two-way filter.

您的指令看起来像

angular.module('myApp').directive('myDirective', function() {
  return {
    require: 'ngModel',
    link: function(scope, element, attrs, ngModelController) {
      ngModelController.$parsers.push(function(data) {
        //convert data from view format to model format
        return data; //converted
      });

      ngModelController.$formatters.push(function(data) {
        //convert data from model format to view format
        return data; //converted
      });
    }
  }
});

HTML

<input my-directive type="text" data-ng-model="entity.date" /> 

下面是一个工作的jsfiddle 例子。

这篇关于在输入元素使用angularjs过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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