Add more text after using a filter in ng-bind in angularjs
Asked Answered
S

2

62

So I want to put a variable through a filter inthe ng-bind directive

ng-bind="input | filter"

but I want to insert more text

ng-bind="input | filter + 'more' "

but this isn't working. Is there a way to add more text in ng-bind, like you could if you were simply using {{}}:

{{input | filter}} more
Schrick answered 28/6, 2014 at 4:6 Comment(0)
I
116

Instead of interpolating(using {{}}) something in the ng-bind directive you can simply enclose the filtered value with a parenthesis and append your text.

<h1 ng-bind="(input | filter) + ' more stuff'"></h1>

furthermore, if the text you want to add is not in any way dynamic then I suggest you append another element to bind the filtered value and then add the text after that element.

e.g.

<h1><span ng-bind="(input | filter)"></span> more stuff</h1>

This saves you one concatenation process.

Example here

Interior answered 28/6, 2014 at 5:2 Comment(1)
Oh, I didn't think to do that, haha. This is a better method than mine.Counterclaim
C
4

You can do something like this:

<h1 ng-bind="'{{input | filter}}' + ' more stuff'"></h1>

Here's an example: http://plnkr.co/edit/rEva7FTPFtr3im7RUlQk?p=preview

Counterclaim answered 28/6, 2014 at 4:40 Comment(1)
There's a caveat in this: it doesn't update when the input value changes. It's essentially becomes static after the first interpolation.Schrick

© 2022 - 2024 — McMap. All rights reserved.