0

I am using angular js and mongoDB, In mongoDB I have some text with \n, So each line comes into the new line based on \n, But I am also want to add line number for each line.

HTML CODE

{{sorceText}}

Controller code

 $scope.sorceText=" ANB+IO:UI+OPO++7866:1111222'\OKP+JJJJ+PP++IOOIO:9989+KKKKKK+II+22:33'
 IIOI+IOIOOI+OOOO:13:1:IA+AA346+4'
 MSG+8'
 LLL+PLPL:MLML+52519950'
 NBK+290818:0000+MJL+LKL+OK+91'
 KWNN+250'
 NFR'
 KK+KK:MMM'"

Displaying Now:

     ANB+IO:UI+OPO++7866:1111222'\OKP+JJJJ+PP++IOOIO:9989+KKKKKK+II+22:33'
     IIOI+IOIOOI+OOOO:13:1:IA+AA346+4'
     MSG+8'
     LLL+PLPL:MLML+52519950'
     NBK+290818:0000+MJL+LKL+OK+91'
     KWNN+250'
     NFR'
     KK+KK:MMM'

Expected I want to be add line number of each line.

  1. ANB+IO:UI+OPO++7866:1111222'\OKP+JJJJ+PP++IOOIO:9989+KKKKKK+II+22:33'
  2. IIOI+IOIOOI+OOOO:13:1:IA+AA346+4'
  3. MSG+8'
  4. LLL+PLPL:MLML+52519950'
  5. NBK+290818:0000+MJL+LKL+OK+91'
  6. KWNN+250'
  7. NFR'
  8. KK+KK:MMM'
1
  • What is {{sourceText}} wrapped in? A pre tag or something else? Basically you will need to split the string into the an array on the new line character. Then bind that to an appropriately styled list. Commented Dec 19, 2018 at 5:56

2 Answers 2

1

it will be helpful to you

 var app=angular.module('myapp',[]);
 app.controller("myctrl",function($scope){
 $scope.sorceText="ANB+IO:UI+OPO++7866:1111222'\OKP+JJJJ+PP++IOOIO:9989+KKKKKK+II+22:33'\nIIOI+IOIOOI+OOOO:13:1:IA+AA346+4'\nMSG+8'\nLLL+PLPL:MLML+52519950'\nNBK+290818:0000+MJL+LKL+OK+91'\nKWNN+250'\nNFR'\nKK+KK:MMM'"
 
 $scope.sorceText_arr=[];
 var arr=$scope.sorceText.split("\n");
 $scope.sorceText_arr=arr;
 })
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>

<div ng-app="myapp" ng-controller="myctrl">
         <div ng-repeat="i in sorceText_arr">
         <div>{{$index+1}} - {{i}}</div>
         </div>
</div>

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the answer. But I am not getting \n, Because \n converting into new in controller itself when I am fetching from DB
so how do you know its going to be a next line, because its single line
When I am printing after call, It showing me in next line
You can check me new question for reference
can we put these line into the array
|
1

Use ol

See working code

In js split to array this string.

$scope.items=$scope.sorceText.split('\n');

In html use loop on this array:

<ol>
  <li ng-repeat="item in items">{{item}}</li>
</ol>

1 Comment

Thanks for the comment. But it is not working for me, I am not getting \n in the controller, \n converting into next line itself

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.