Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a prefix/suffix to value of input tag

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

<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).

I haven't found any css/Angular tricks yet... any ideas?

like image 439
Uria Mor Avatar asked Jun 24 '15 06:06

Uria Mor


People also ask

How do you add suffix to text in HTML?

HTML <sup> Tag.

Can you put a div in a input?

You can't place any element inside an input. An input does not contain any HTML. Show activity on this post. The way to do that is to put the input and the wanted div inside a wrapper div, which should look like the input - the wanted width, length of the input and should have border.


1 Answers

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.

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)

However, there's a workaround. Use ng-bind on the variable and mention prefix/suffix text separately in the value="..." attribute.

<input type="text" value="prefixText {{name}} suffixText" ng-bind="name">

Here's the demo

like image 55
nalinc Avatar answered Sep 19 '22 05:09

nalinc