在输入上NG-模型过滤器 [英] filters on ng-model in an input

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

问题描述

我有一个文本输入,我不希望让用户使用的空间,一切类型将变为小写。

I have a text input and I don't want to allow users to use spaces, and everything typed will be turned into lowercase.

我知道我不能在NG-模型如使用过滤器。

I know I'm not allowed to use filters on ng-model eg.

ng-model='tags | lowercase | no_spaces'

我看了看创造我自己的指令,但增加功能,以 $解析器 $格式化没更新输入,只有有 NG-模型上的其他元素。

I looked at creating my own directive but adding functions to $parsers and $formatters didn't update the input, only other elements that had ng-model on it.

我怎样才能改变我目前在打字?输入

How can I change the input of that I'm currently typing in?

我基本上是试图创建标签功能,该功能的工作原理就像这里一个在计算器上。

I'm essentially trying to create the 'tags' feature that works just like the one here on StackOverflow.

推荐答案

我会建议看模型值和更新它在CHAGE:<一href=\"http://plnkr.co/edit/Mb0uRyIIv1eK8nTg3Qng?p=$p$pview\">http://plnkr.co/edit/Mb0uRyIIv1eK8nTg3Qng?p=$p$pview

I would suggest to watch model value and update it upon chage: http://plnkr.co/edit/Mb0uRyIIv1eK8nTg3Qng?p=preview

唯一有趣的问题是与空间:在AngularJS输入1.0.3 NG-模型自动裁剪字符串,因此它不检测模型改变,如果你在年底或在启动时添加空格(空格所以不会被自动我的code删除)。但是,在1.1.1有'NG微调'指令,允许禁用此功能(<一个href=\"https://github.com/angular/angular.js/commit/d519953a4b219035587e3fcb2e9cc52e02b408ca\">commit).所以我决定用1.1.1来实现你在你的问题中描述具体的功能。

The only interesting issue is with spaces: In AngularJS 1.0.3 ng-model on input automatically trims string, so it does not detect that model was changed if you add spaces at the end or at start (so spaces are not automatically removed by my code). But in 1.1.1 there is 'ng-trim' directive that allows to disable this functionality (commit). So I've decided to use 1.1.1 to achieve exact functionality you described in your question.

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

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