javascript 为输入标签的值添加前缀/后缀
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31019643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add a prefix/suffix to value of input tag
提问by Uria Mor
I have this situation: I need to add quotes inside an input text field (html) without changing the value of the input. I'm working with angular so I use ngModel, it looks like this
我有这种情况:我需要在输入文本字段 (html) 中添加引号而不更改输入的值。我正在使用 angular 所以我使用 ngModel,它看起来像这样
<input ng-model="data" type="text" />
I want the input field to show "whatever is in {{data}}
" but the variable data itself remains unchanged (no quotes).
我希望输入字段显示“任何内容{{data}}
”,但变量数据本身保持不变(没有引号)。
I haven't found any css/Angular tricks yet... any ideas?
我还没有找到任何 css/Angular 技巧……有什么想法吗?
采纳答案by nalinc
Using ng-model="data"
in <input type="text">
binds the data
with entire text field. This is not particularly useful in situations where you want only a portion of text(being displayed in text field) to get bind with the scope.
使用ng-model="data"
in与整个文本字段<input type="text">
绑定。在您只希望文本的一部分(显示在文本字段中)与范围绑定的情况下,这不是特别有用。data
For instance, if you do
例如,如果你这样做
<input type="text" value="prefixText {{name}} suffixText" ng-model="name">
The input box will display whatever is in name
(with no prefix/suffix text)
输入框将显示任何内容name
(没有前缀/后缀文本)
However, there's a workaround. Use ng-bind
on the variable and mention prefix/suffix text separately in the value="..."
attribute.
但是,有一个解决方法。ng-bind
在变量上使用并在value="..."
属性中单独提及前缀/后缀文本。
<input type="text" value="prefixText {{name}} suffixText" ng-bind="name">
Here's the demo
这是演示