博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
angularJS 中的two-way data binding.
阅读量:4357 次
发布时间:2019-06-07

本文共 3267 字,大约阅读时间需要 10 分钟。

原文: 

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

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

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

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

The  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.) –  
  • 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 . You can name the 4th parameter whatever you want. (I would name it ngModelCtrl.) –  
  • 7
    This technique is documented at , in the Custom Validation section. –  
  • 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 –   
  • 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. –  
  •  
    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. –  
  •  
    +1 for (text || ''), aha moment –  
  •  
    What is If I want to show the lowercase letters while typing (i.e., on-change) itself. –   
  •  
    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 –  
  •  
    @phaas thx for answers was looking for the same from last 2 days –   

转载于:https://www.cnblogs.com/oxspirt/p/9240525.html

你可能感兴趣的文章
[LeetCode] Two Sum
查看>>
Linux dd命令制作U盘启动盘
查看>>
Docker for windows 入门二(Kitematic的使用)
查看>>
RHEL 7 安装步骤详解
查看>>
TCP三次握手应用及原理
查看>>
ARP协议揭密
查看>>
自动同步自动备份两台电脑文件的方法
查看>>
【BZOJ4816】【SDOI2017】数字表格 [莫比乌斯反演]
查看>>
安装xampp后,出现“Apache 2 Test Page powered by CentOS“
查看>>
控件属性使用代码动代绑定
查看>>
Linux下查看文件内容的命令
查看>>
子网划分
查看>>
Python系列文章索引
查看>>
iMuseum
查看>>
xshell & xftp
查看>>
Cassandra1.2文档学习(16)—— 模式的变化
查看>>
Linux文件系统
查看>>
001---Python简介
查看>>
第一次JAVA作业
查看>>
解决请求接口要以JSON格式请求后端的问题
查看>>