1

I have the following snippet of code that creates 1, 2, 3 etc based on the number in parent repeat (All good).

{{$index+1}}

But I am looking for Text such as One, Two Three etc

Is there a way to achieve this in Angular? (Text instead of numerics)

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
Jasmine
  • 5,186
  • 16
  • 62
  • 114
  • try this answer [Angular directive/service to convert number into words](http://stackoverflow.com/questions/34649013/angular-directive-service-to-convert-number-into-words-need-in-angularjs) – anoop m m Jun 17 '16 at 06:36
  • @anoopmm: Thanks, how to use this javascript? I am new...I have an html5/angular html5 page and a JS file that has controller etc....But my {{index+1}} is in html file...how to use it? – Jasmine Jun 17 '16 at 06:43
  • I sthis an Angularjs or an Angular2 question? – Günter Zöchbauer Jun 17 '16 at 06:53
  • similar question here hope it helps http://stackoverflow.com/questions/34649013/angular-directive-service-to-convert-number-into-words-need-in-angularjs?lq=1 – Vicky Kumar Jun 17 '16 at 06:59

2 Answers2

0

One solution is to create a filter, for example:

// Script.js
angular.module('app')
    .filter('numberToWord', function() {
        return function( s ) {

            var th = ['','thousand','million', 'billion','trillion'];
            var dg = ['zero','one','two','three','four', 'five','six','seven','eight','nine']; 
            var tn = ['ten','eleven','twelve','thirteen', 'fourteen','fifteen','sixteen', 'seventeen','eighteen','nineteen'];
            var tw = ['twenty','thirty','forty','fifty', 'sixty','seventy','eighty','ninety'];

            s = s.toString(); 
            s = s.replace(/[\, ]/g,''); 
            if (s != parseFloat(s)) return 'not a number'; 
            var x = s.indexOf('.'); 
            if (x == -1) x = s.length; 
            if (x > 15) return 'too big'; 
            var n = s.split(''); 
            var str = ''; 
            var sk = 0; 
            for (var i=0; i < x; i++) 
            {
                if ((x-i)%3==2) 
                {
                    if (n[i] == '1') 
                    {
                        str += tn[Number(n[i+1])] + ' '; 
                        i++; 
                        sk=1;
                    }
                    else if (n[i]!=0) 
                    {
                        str += tw[n[i]-2] + ' ';
                        sk=1;
                    }
                }
                else if (n[i]!=0) 
                {
                    str += dg[n[i]] +' '; 
                    if ((x-i)%3==0) str += 'hundred ';
                    sk=1;
                }

                if ((x-i)%3==1)
                {
                    if (sk) str += th[(x-i-1)/3] + ' ';
                    sk=0;
                }
            }
            if (x != s.length)
            {
                var y = s.length; 
                str += 'point '; 
                for (var i=x+1; i<y; i++) str += dg[n[i]] +' ';
            }
            return str.replace(/\s+/g,' ');
        };
    })

And then you can use it like this:

{{$index | numberToWord }}

Edit: An example Plunker http://plnkr.co/edit/DIQ3YVkH7N6PUaqju82X?p=preview

Prince John
  • 642
  • 1
  • 4
  • 17
  • Prince, it always says that "not a number" – Jasmine Jun 17 '16 at 07:04
  • @Learner Please, check the Plunker in my edited answer – Prince John Jun 17 '16 at 07:10
  • ok gotcha, the problem is, I am using strict comparison as otherwise my grunt fails and doesn't let me checkin, as a good practice, we use === and hence in the line not a number in the function, we use !== and it is screwing it up. When I remove one = and keep the original function as above, it does says a error saying tries to coier values – Jasmine Jun 17 '16 at 07:14
  • Is there a solution to use strict === and also avoid the problem here? – Jasmine Jun 17 '16 at 07:16
  • Also, when I run grunt, it throws heaps of error in this function and my project will not allow me to checkin code without resolving all these errors. Please run grunt and see what errors you get – Jasmine Jun 17 '16 at 07:17
  • @Learner in the Plunker I have replaced "==" with "===" and "!=" with "!==" – Prince John Jun 17 '16 at 07:19
  • Prince, I did that myself, still HEAPS of error while running GRUNT. HEAPS. In every words of this function. Crazy – Jasmine Jun 17 '16 at 15:05
  • @Learner Impossible to help you further without you sharing your code. However, your original question is answered. – Prince John Jun 18 '16 at 08:13
  • Prince, please run the grunt it will show you the errors – Jasmine Jun 19 '16 at 12:25
-1

There is no standard feature in Angular for that. Here is a link to a great algorithm to convert number to word Developers blog - Convert Numbers into Words Using JavaScript

Put the algorithm in a function and use it like this :

{{convertNumberToWord($index+1)}}

Here is a working example in Plunker for your situation

Tom Shen
  • 1,045
  • 3
  • 11
  • 29
  • sorry when I put everything inside a function, it says local function towords is never used. What should I do? – Jasmine Jun 17 '16 at 06:35
  • I am learning javascript. Please excuse for silly questions. Now we have Angular html file, javascript files already which has controller, module etc. I created another JS file and copied the stuff in above link and then calling that like toWords($index+1), its not working.... How should I correct? – Jasmine Jun 17 '16 at 06:38
  • I have added a working example for your situation in Plunker. – Tom Shen Jun 17 '16 at 06:43
  • My grunt shows me HEAPS of errors on this function in the link and I couldn't use it. Neither the output is one, two, three etc, actually nothing shows up – Jasmine Jun 17 '16 at 06:51
  • tom, thanks, what is $scope.items = [1,2,3,4,5]; ? Also, I already have a JS controller, should I just put these inside that controller like you did in app,js and try? – Jasmine Jun 17 '16 at 06:57
  • $scope.items is just an array for the ng-repeat to generate the $index. You can just put the functions in your controller. No need to put $scope.items in it. If you need help, can you link your controller? – Tom Shen Jun 17 '16 at 06:59
  • Can you help me get rid of grunt errors? I have every line errors on this function you gave me. Please run grunt and see what errors – Jasmine Jun 17 '16 at 07:18
  • Can you provide your code? I can't help you if I don't know how your code looks like. – Tom Shen Jun 17 '16 at 07:20
  • Tom, its all good serves the purpose, but so many grunt errors like use strict guidelines etc. What is your suggestion – Jasmine Jun 20 '16 at 01:50