Add a prefix/suffix to value of input tag

25,882

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

Share:
25,882

Related videos on Youtube

Uria Mor
Author by

Uria Mor

I'm a math undergraduate at Tel-Aviv University. Interested in math, computer-science, web development, and I'm also a Linux enthusiast.

Updated on January 07, 2021

Comments

  • Uria Mor
    Uria Mor over 3 years

    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?

  • Uria Mor
    Uria Mor almost 9 years
    is there a way to make the prefixText suffixText surround the text where I'm typing it?
  • nalinc
    nalinc almost 9 years
    The first text-box where you are typing is provided to let user change the name and see its effect in second text-box
  • Uria Mor
    Uria Mor almost 9 years
    I want all in one... i.e the pre/suffixes should surround the text as I type it. without changing {{data}}
  • Sabyasachi
    Sabyasachi almost 8 years
    @NLN...This is just awesome!
  • NoNameProvided
    NoNameProvided over 3 years
    The question is about AngularJS (v1) and your answer is for Angular (v2+) also the demo link is broken.
  • Rizwan Ali
    Rizwan Ali over 3 years
    In Question haven't mention the angular version Anyway it will work for Angular 2+
  • NoNameProvided
    NoNameProvided over 3 years
    The tag mentions it because it's tagged with angularjs. And you clearly use the Material package created for Angular (v2+) so no, your answer won't work.
  • MarkMYoung
    MarkMYoung over 2 years
    Thank you, @RizwanAli. This was a Google search result specifically for Angular 12 and was helpful to me (despite the question being for AngularJS v1.x).