原文: https://stackoverflow.com/questions/11616636/how-to-do-two-way-filtering-in-angularjs

------------------------------------------------------------------------------------------------------------

It turns out that there's a very elegant solution to this, but it's not well documented.

Formatting model values for display can be handled by the | operator and an angular formatter. It turns out that the ngModel that has not only a list of formatters but also a list of parsers.

1. Use ng-model to create the two-way data binding

<input type="text" ng-model="foo.bar"></input>

2. Create a directive in your angular module that will be applied to the same element and that depends on the ngModel controller

module.directive('lowercase', function() {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, element, attr, ngModel) {
...
}
};
});

3. Within the link method, add your custom converters to the ngModelcontroller

function fromUser(text) {
return (text || '').toUpperCase();
} function toUser(text) {
return (text || '').toLowerCase();
}
ngModel.$parsers.push(fromUser);
ngModel.$formatters.push(toUser);

4. Add your new directive to the same element that already has the ngModel

<input type="text" lowercase ng-model="foo.bar"></input>

Here's a working example that transforms text to lowercase in the input and back to uppercase in the model

The API Documentation for the Model Controller also has a brief explanation and an overview of the other available methods.

answered Oct 18 '12 at 5:55
phaas

2,3511106
  •  
    IS there any reason you used "ngModel" as the name for the fourth parametet in your linking function? Isn't that just a generic controller for the directive that has basically nothing to do with the ngModel attribute? (Still learning angular here so I could be totally wrong.) – Drew Miller Dec 17 '12 at 1:52
  • 6
    Because of "require: 'ngModel'", the linking function's 4th parameter will be the ngModel directive's controller -- i.e., foo.bar's controller, which is an instance of ngModelController. You can name the 4th parameter whatever you want. (I would name it ngModelCtrl.) – Mark Rajcok Dec 19 '12 at 22:40
  • 7
    This technique is documented at docs.angularjs.org/guide/forms, in the Custom Validation section. – Nikhil Dabas Feb 12 '13 at 19:49
  • 1
    @Mark Rajcok in the fiddle provided, while clicking Load Data -- all lowercase, i expected the model value would be in ALL CAPS, but the model value was in small. Could you pls. explain why, and how to make the model always IN CAPS – rajkamal Apr 12 '13 at 19:58 
  • 1
    @rajkamal, since loadData2() modifies $scope directly, that's what the model will be set to... until the user interacts with the textbox. At that point, any parsers can then affect the model value. In addition to a parser, you could add a $watch to your controller to transform the model value. – Mark Rajcok Apr 12 '13 at 21:06
  •  
    Hi Guys, i am new to angular and struck in ngModel, This explanation is Ok, but what i again feel, is that we can use $filters in directive like $filter('uppercase')(value) or $filter('lowercase')(value);and can do the operation that is performed using ngModel.$parsers.push(fromUser); ngModel.$formatters.push(toUser);, So when/why exactly we need ngModel. Note: This may be a silly question or not valid, But please correct me. – RAVI MONEApr 4 '14 at 12:16 
  •  
    +1 for (text || ''), aha moment – Mahes Apr 9 '14 at 15:21
  •  
    What is If I want to show the lowercase letters while typing (i.e., on-change) itself. – Kishore Relangi Jul 21 '15 at 11:46 
  •  
    thank you so much! i was searching for this neat and conciese example for 8 hours or so! the formatters and parsers seem to be a powerfull feature like JSF converters in java – david.lucky Oct 7 '15 at 11:04
  •  
    @phaas thx for answers was looking for the same from last 2 days – Yogesh Mar 10 '16 at 7:21 

angularJS 中的two-way data binding.的更多相关文章

  1. Android开发教程 - 使用Data Binding(六)RecyclerView Adapter中的使用

    本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...

  2. Android开发教程 - 使用Data Binding(四)在Fragment中的使用

    本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...

  3. Android开发教程 - 使用Data Binding(三)在Activity中的使用

    本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...

  4. Android Data Binding Library

    Data Binding Library Data Binding Library是一个支持库,允许您使用声明格式(而不是编程)将布局中的UI组件与应用程序中的数据源绑定. 布局通常在调用UI框架方法 ...

  5. Android开发教程 - 使用Data Binding Android Studio不能正常生成相关类/方法的解决办法

    本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...

  6. Android开发教程 - 使用Data Binding(七)使用BindingAdapter简化图片加载

    本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...

  7. Android开发教程 - 使用Data Binding(八)使用自定义Interface

    本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...

  8. Android开发教程 - 使用Data Binding(五)数据绑定

    本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...

  9. Android开发教程 - 使用Data Binding(二)集成与配置

    本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...

  10. Android开发教程 - 使用Data Binding(一) 介绍

    本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...

随机推荐

  1. 细说robots.txt

    robots.txt Robots协议(也称为爬虫协议.机器人协议等)的全称是“网络爬虫排除标准”(Robots Exclusion Protocol),网站通过Robots协议告诉搜索引擎哪些页面可 ...

  2. 《Java编程思想》笔记 第十五章 泛型

    1 泛型 “泛型”意思就是适用于许多类型. 使用泛型的目的之一: 指定容器持有什么类型,让编译器确保正确性,而不是在运行期发现错误. 这个容器可以看成是有其他类型对象作为成员的类,而不单单只是JDK中 ...

  3. redis使用中的常见错误

    1.2016年12月17日  启动redis报错,错误信息如下: 解决办法:redis没有正常关闭(redis安装在虚拟机上,直接杀死了虚拟机进程) 导致redis.pid文件一直被锁定,重启redi ...

  4. poj 3254(状态压缩+动态规划)

    http://poj.org/problem?id=3254 题意:有一个n*m的农场(01矩阵),其中1表示种了草可以放牛,0表示没种草不能放牛,并且如果某个地方放了牛,它的上下左右四个方向都不能放 ...

  5. cobbler自动重装

    如果物理机上想更换操作系统 yum -y install http://mirrors.163.com/centos/7/extras/x86_64/Packages/epel-release-7-9 ...

  6. Python:文件操作技巧(File operation)(转)

    Python:文件操作技巧(File operation) 读写文件 # ! /usr/bin/python #  -*- coding: utf8 -*- spath = " D:/dow ...

  7. unused import statement android studio 解决方法

    解决方法:“file”-->“invalidate caches/restart” 解决 感谢大神的解答

  8. 训练指南 UVALive - 5713(最小生成树 + 次小生成树)

    layout: post title: 训练指南 UVALive - 5713(最小生成树 + 次小生成树) author: "luowentaoaa" catalog: true ...

  9. Codeforces #447 Div2 E

    #447 Div2 E 题意 给出一个由有向边构成的图,每条边上有蘑菇,假设有 \(n\) 个蘑菇,那么第一次走过这条边可以获得 \(n\) 个蘑菇,第二次 \(n-1\),第三次 \(n-1-2\) ...

  10. c程序设计语言 导言

    重在实践!! 1-10 P13 #include <stdio.h> int main() { int c; while((c = getchar()) != EOF) { ; if (c ...