angularJS 中的two-way data binding.
原文: 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.
- 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
- 6Because 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 - 7This 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
$scopedirectly, 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.的更多相关文章
- Android开发教程 - 使用Data Binding(六)RecyclerView Adapter中的使用
本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...
- Android开发教程 - 使用Data Binding(四)在Fragment中的使用
本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...
- Android开发教程 - 使用Data Binding(三)在Activity中的使用
本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...
- Android Data Binding Library
Data Binding Library Data Binding Library是一个支持库,允许您使用声明格式(而不是编程)将布局中的UI组件与应用程序中的数据源绑定. 布局通常在调用UI框架方法 ...
- Android开发教程 - 使用Data Binding Android Studio不能正常生成相关类/方法的解决办法
本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...
- Android开发教程 - 使用Data Binding(七)使用BindingAdapter简化图片加载
本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...
- Android开发教程 - 使用Data Binding(八)使用自定义Interface
本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...
- Android开发教程 - 使用Data Binding(五)数据绑定
本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...
- Android开发教程 - 使用Data Binding(二)集成与配置
本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...
- Android开发教程 - 使用Data Binding(一) 介绍
本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...
随机推荐
- 【转】spring 装配Bean中构造参数的注入
转载自:http://www.bianceng.cn/Programming/Java/201307/37027.htm spring 装配Bean中构造参数的注入 spring装配bean中还有一种 ...
- [ 总结 ] Linux下两种常用的双网卡绑定
1. mode=0 (round-robin) 链路聚合:将两个或者更多数据信道结合成一个单一的信道,该信道以一个更高带宽的逻辑链路出现,链路聚合一般用来连接一个或多个带宽需求量大的设备,链路聚合是指 ...
- [ 手记 ] Oracle 11g安装过程
安装环境: 操作系统:Centos6.4 Desktop 主机名:oracle 内存:2G 安装前准备: 修改主机名: [root@oracle ~]# vim /etc ...
- 51nod 几道题
1001 数组中和等于K的数对 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 给出一个整数K和一个无序数组A,A的元素为N个互不相同的整数, 找出数组A中所有和等于 ...
- SQLAlchemy技术文档(中文版)-上
转自:http://www.cnblogs.com/iwangzc/p/4112078.html 1.版本检查 import sqlalchemy sqlalchemy.__version__ 2.连 ...
- mysql之any,some all(zz)
转载自:http://blog.csdn.net/netcy/article/details/8464503 ALL和ANY操作符的常见用法是结合一个相对比较操作符对一个数据列子查询的结果进行测试.它 ...
- java中Math.abs(-2147483648)的返回值应该是什么?
我觉得这是一个非常有意思的问题,Math.abs(-2147483648)的返回值应该是什么? java计算结果 为什么没有得到正数结果呢? 首先我们先看下java区分整数正负的原理.在二进制的情况下 ...
- React-Native集成dva.js
dvajs作为一个基于redux的状态管理框架,在react中的表现还是很不错的,如果我们想要在react-native应用中使用dvajs该怎么做呢? 首先安装dva-core和react-redu ...
- unity制作简单血条
学习Unity已经10天了,也没发现有什么长进,真的急.昨天仿着官方Demo做了个射击游戏轮廓,其中需要给每个怪做一个血条. 搜了一些,挺复杂的,用NGUI或者UGUI,外加很长的代码...不过还是找 ...
- 树上两点的最近公共祖先问题(Least Common Ancestors)
概念: 对于有根树T的两个节点u,v,最近公共祖先LCA(T, u, v)表示一个节点 x, 满足 x 是 u , v 的祖先且 x 的深度尽可能的大.即从 u 到 v 的路径一定经过点 x. 算法: ...