1

I am new to angular.js and I'm having an issue with converting string into array and displaying it with ng-repeat, so far i got this custom filter from a stackoverflow question and the codes are:

JS:

var app = angular.module('globalModule', [])

    app.controller("SampleController", function($scope){

        $scope.data = "123abc123";
    }); 


    app.filter("spaceBreak", 
        function () {
            return function ( value ) {
                  if( !value.length ) return;
                      return value.split("");   
            }
        });

HTML:

 <body ng-controller="SampleController">
     <h4 id="id_{{$index}}" ng-repeat="value in data | spaceBreak"><span ng-bind="value"></span></h4>
</body>

My problem is, it converts and display properly if it is an alphabet(abc) or a number(123) alone, and if combined (abc123) or (123abc) still good, but if the data is number+alphabet+number (123abc123) it doesnt show anymore in ng-repeat. I am really lost and really need help. Hope someone can lend me an answer.

2
  • A little confused... so you would want the string 123abc123 to be split into an array and show up in your ng-repeat as 123, abc, 123? Commented Mar 13, 2016 at 18:23
  • Nope, I want to split it individually, like 1,2,3,a,b,c,1,2,3 splitting abc123 and 123abc has no issue, but with 123abc123 it doesnt show. i dont know why, same with characters, like !abc! it doesnt show in ng-repeat. Commented Mar 13, 2016 at 18:41

1 Answer 1

2

I think what you are looking for is the track by $index functionality. You code errors out because you have duplicates in your $scope.data. That filter isn't really doing anything for you, so you can just ng-repeat through your $scope.data string.

var app = angular.module('globalModule', [])

app.controller("SampleController", function($scope){

  $scope.data = "123abc123"; 
})
<!DOCTYPE html>
<html ng-app="globalModule">

  <head>
    <script data-require="[email protected]" data-semver="1.5.0" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body ng-controller="SampleController">
    <h4 id="id_{{$index}}" ng-repeat="value in data track by $index"><span ng-bind="value"></span></h4>
  </body>

</html>

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

4 Comments

Nice answer, I didn't know that ng-repeat would iterate over strings too. One question, what is lodash being used for here?
Sorry, it's not. I autopilot add it to every snippet, haha. Will remove, thanks.
Also, for the OP, here is the official Angular explanation of why duplicates in an ng-repeat is a problem.
Ohh you save me man! thank you very much! Adding track by $index made it work thank youu

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.