在输入中过滤 ng-model [英] filters on ng-model in an input

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

问题描述

我有一个文本输入,我不想让用户使用空格,输入的所有内容都会变成小写.

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-model 上使用过滤器,例如.

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

ng-model='tags | lowercase | no_spaces'

我考虑创建自己的指令,但是向 $parsers$formatters 添加函数并没有更新输入,只有其他具有 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?

我实际上是在尝试创建与 StackOverflow 上的功能一样的标签"功能.

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

推荐答案

我建议查看模型值并在更改时更新它:http://plnkr.co/edit/Mb0uRyIIv1eK8nTg3Qng?p=preview

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

唯一有趣的问题是空格:在 AngularJS 1.0.3 ng-model 中,输入会自动修剪字符串,因此如果在末尾或开头添加空格,它不会检测到模型已更改(因此空格不会自动被我的代码删除).但是在 1.1.1 中有允许禁用此功能的ng-trim"指令(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-model的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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