在输入中对ng模型进行过滤(filters on ng-model in an input)

我有一个文本输入,我不想允许用户使用空格,所有输入的内容都将变成小写。

我知道我不允许在ng模型上使用过滤器,例如。

ng-model='tags | lowercase | no_spaces'

我看到创建自己的指令,但是向$parsers添加函数,而$formatters没有更新输入,只有其他元素具有ng-model 。

如何更改我当前输入的输入?

我本质上试图创建一个像StackOverflow这样的“标签”功能。

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

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

ng-model='tags | lowercase | no_spaces'

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: http ://plnkr.co/edit/Mb0uRyIIv1eK8nTg3Qng? p= preview

唯一有趣的问题是空格:In AngularJS 1.0.3 ng-model在输入上自动修剪字符串,因此如果在结尾或开始时添加空格,则不会检测到该模型已更改(因此,空格不会被我自动删除码)。 但在1.1.1中有一个允许禁用此功能( 提交 )的'ng-trim'指令。 所以我决定使用1.1.1来实现您在问题中描述的准确功能。

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

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模型进行过滤(filters on ng-model in an input)

我有一个文本输入,我不想允许用户使用空格,所有输入的内容都将变成小写。

我知道我不允许在ng模型上使用过滤器,例如。

ng-model='tags | lowercase | no_spaces'

我看到创建自己的指令,但是向$parsers添加函数,而$formatters没有更新输入,只有其他元素具有ng-model 。

如何更改我当前输入的输入?

我本质上试图创建一个像StackOverflow这样的“标签”功能。

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

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

ng-model='tags | lowercase | no_spaces'

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: http ://plnkr.co/edit/Mb0uRyIIv1eK8nTg3Qng? p= preview

唯一有趣的问题是空格:In AngularJS 1.0.3 ng-model在输入上自动修剪字符串,因此如果在结尾或开始时添加空格,则不会检测到该模型已更改(因此,空格不会被我自动删除码)。 但在1.1.1中有一个允许禁用此功能( 提交 )的'ng-trim'指令。 所以我决定使用1.1.1来实现您在问题中描述的准确功能。

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

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.