AngularJS append text to ng-bind with filter
Asked Answered
M

4

7

I have this code (output=1,000):

<span ng-bind"item.num | number : 0"></span>

But i want something like 1,000 km. Any way to do this without create a new span.

Something like this isn't working:

<span ng-bind"item.num + ' km' | number : 0"></span>
Mcquoid answered 10/8, 2015 at 15:11 Comment(1)
possible duplicate of Add more text after using a filter in ng-bind in angularjsTyrosine
S
9
<span ng-bind="(input | filter) + 'km'"></span>
Stidham answered 10/8, 2015 at 15:20 Comment(0)
P
3

The syntax for this is :

<span ng-bind="(item.num | number : 0) + ' km' "></span>

Working Plunkr

Phillipphillipe answered 10/8, 2015 at 15:18 Comment(0)
R
2

As a more generic solution, specify a custom filter JSFiddle:

.filter('formatNumber', function () {
    return function (input) {
        return input + 'km';
    }
});

And:

<span ng-bind="item.num | formatNumber"></span>
Ramage answered 10/8, 2015 at 15:20 Comment(1)
it could be done on HTML would be more better using number filter itself like this https://mcmap.net/q/1426425/-angularjs-append-text-to-ng-bind-with-filterStearns
T
1

You can do this by using parenthesis.

<span ng-bind"(item.num | number : 0) + 'km' "></span>

If the unit is always km and is not dynamic, you can just put it in the regular text.

<div><p><span ng-bind"item.num | number : 0"></span>km</p></div>
Tyrosine answered 10/8, 2015 at 15:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.